Regarding login into a webpage using java code ?

Hi,

I am trying to connect a web page through java code, using HttpURLConnection api,

I am able to access its welcoe page. But this page is asking for username and password.

Can any one tell me how I can login into the website through java code. The web page is

using the post method to send request to the server.

- Vikas

[360 byte] By [vikassahua] at [2007-10-3 2:55:10]
# 1
java.net.Authenticator
ejpa at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 2
Hi, Using your sugesstion, I am able to login into the webpage but my next page is not being display.
vikassahua at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 3
Hey,I have a similar requirement, can you please send me the same code.Highly appreciate it....Send the code to md.nadeem@yahoo.comThanks,Bye
NadeemAhmeda at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 4
Can you please send me the code to myself@akashagrawal.com ?
akash1979a at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 5
I am also having same problem can u mail me the code for entering into website through standalone java code
Nikkua at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 6
can anybody help mei am also facing same problem.i want to login into website through codeand then select some raduio button and click on one button.can u mail me the same code my mailId isanil_sr84@rediffmail.comMessage was edited by:
Nikkua at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 7
hi vikas i am new to javacan u tell me the solution of ur problem.i m also having same problem
Nikkua at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 8

When you have logged in, the server has to track the identity of the client. It is called session. As HTTP is a state-less protocol, some effort is needed. A widespread method to do this is to use cookies. The server sends a cookie (named SESSIONID if memory serves me right). The client sends it back in the header in every request. So the server can keep track of the client session based on this value.

BIJ001a at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 9

Accessing the protected resource depends on the authentication method.If it is using BASIC then u can set the Authorization header to the encoded user name and password.For Form based login we don't use this approach.We should have to fill the form component with the required values before posting it.I've a code snippet that works for BASIC.

URL url = new URL("some url");

// Open a HTTP connection to the URL

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

// Allow Inputs

conn.setDoInput(true);

// Allow Outputs

conn.setDoOutput(true);

// Don't use a cached copy.

conn.setUseCaches(false);

// Use a post method.

conn.setRequestMethod("POST");

conn.setRequestProperty("Connection", "Keep-Alive");

// Authorization [Base64Encoder - user defined class to encode]

conn.setRequestProperty("Authorization", "Basic "+ Base64Encoder.encode("username:password"));

conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

dos = new DataOutputStream(conn.getOutputStream());

StringBuffer content = new StringBuffer(1024);

content.append("textboxname1=" + +URLEncoder.encode("value1"));

content.append("textboxname2=" +URLEncoder.encode("value2"));

dos.writeBytes(content.toString());

dos.flush();

--

nt code;

if ((code = conn.getResponseCode()) != 200) {

if(code==401)

//unauthorized user

}

Harish_MCAa at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 10

hi all!

i m wrking on a proj in which i need to login on some website and need to fetch the data from this site.i want to do it on regular basis.

i dnt 9 which type of authentication it is using. how to 9 this?.

And can u pl send me the form based authentication code?

thanx in advance!!!!!!!

neerajAtSDNa at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 11

Hi,

I am trying to connect a web page through java code, using HttpURLConnection api,

I am able to access its welcoe page. But this page is asking for username and password.

Can any one tell me how I can login into the website through java code. The web page is

using the post method to send request to the server.

can u send me the code:

shubhadeep.sengupta@gmail.com

Shubhadeepa at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 12
What problems did you encounter with the suggestions in replies #1 and #9?
ejpa at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 13

in case of form-based authentication, u must know the name of the compponent that takes user name and password along with the action url. Set the value of these component(similiar to my prev reply) before writing it to the data output stream object. Set only the headers which u think is required.

Harish_MCAa at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 14
Can anyone please send me this code....i am failing to give the userid and password in the webpage after logging in....my id is profilemi@gmail.com
profilemia at 2007-7-14 20:44:15 > top of Java-index,Core,Core APIs...
# 15

Hi,

I am using HTTPClient to login to a website. The form specifies POST method. there are 2 hidden types along with username and password used by the form. i have passed all these variables using namevalue pair. However i am not able to login to website. the server returns cookie successfully.

Thanks for the help.

Suveleea at 2007-7-21 10:02:48 > top of Java-index,Core,Core APIs...
# 16
hi I am trying to write the same code but it throws several exceptions. Could you send me the full code please? my e-mail id is : g_nelsonjoseph@yahoo.comso that i can learn the HTTP concept. Please......Thanks in advance-Nelson
NelsonJosepha at 2007-7-21 10:02:48 > top of Java-index,Core,Core APIs...
# 17

I used the following code in the client java-program:

java.net.Authenticator.setDefault(new simpleAuthenticator());

and simpleAuthenticatior is:

import java.net.PasswordAuthentication;

/**

*

* @author jk

* @version

*/

public class simpleAuthenticator extends java.net.Authenticator {

PasswordAuthentication pa;

/** Creates new simpleAuthenticator */

public simpleAuthenticator () {

pa = new PasswordAuthentication ("", "".toCharArray());

}

public simpleAuthenticator (String user, String pwd) {

pa = new PasswordAuthentication (user, pwd.toCharArray());

}

protected PasswordAuthentication getPasswordAuthentication () {

return pa;

}

}

Regards,

Johannes

blacksnakea at 2007-7-21 10:02:48 > top of Java-index,Core,Core APIs...