This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Access web url through java code.   Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Access web url through java code.   " Watch "Access web url through java code.   " New topic
Author

Access web url through java code.

Vikas Sahu
Greenhorn

Joined: Mar 07, 2003
Posts: 29
Hi,

I have one problem. I have one web url htpp://127.0.0.1:8080/UserLogin.jsp
Now I have to acees this url though java code not from the web browser.
When I am accessing this page through HttpURLConnection class,
It is ok, but can any body tell me how I can give user name and password through code for this page UserLogin.jsp.



Regards -<br />Vikas Kumar Sahu
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8263

Are you using form-based authentication? Send the user name and password as request parameters like this example.
If you are using HTTP based authentication, use the Authenticator class as described here.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If login from a browser does POST on a form, you can simulate that nicely with your URLConnection. Set DoInput and DoOutput true, get the output stream from the connection and write your form content. Don't forget to flush and close the stream.

Here's what I write to log on my Wiki ... the form has two hidden fields and the userid & password entry fields:

See the JavaDoc and the Sun Network Tutorial for more details.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35243
    
    7
See this entry of the Applet FAQ, which describes how to do it. In this regard, applets and applications are identical.


Android appsImageJ pluginsJava web charts
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
If you need to write that code for testing purpose HttpUnit can help.

Please read section Testing pool entry in the link below to know if the link helpshttp://httpunit.sourceforge.net/doc/tutorial/task1editor-entry.html
[ September 19, 2006: Message edited by: karthi keyan ]

http://www.skillassert.com


Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2300
If you have idea where the form of login.jsp is hitting then you can fake out the login process and there is no harm in this as you are making a tool not the browser.If you know where the login form sumbits like /cool/verify
and username variable is user and password is pass then you can create the url like /cool/verify?user=<userName>&pass=<password> with the url try creating the HTTPURLConnection , similar is the process .The only thing is that user=<userName>&pass=<password> will be in the body and you have to set the header set-content-length header.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Access web url through java code.
 
Similar Threads
JSF Navigation Issue
War file created by Ant is not working with an exception cannot find global ActionForward for name *
Calling JSP from Servlet
Regarding login into a webpage using java code ?
c:import with Facelets