This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes httpclient athentification issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "httpclient athentification issue" Watch "httpclient athentification issue" New topic
Author

httpclient athentification issue

Alex Ter
Greenhorn

Joined: Sep 30, 2010
Posts: 10
Hi guys, I have an issue to login to a website with a simple form login.

My code always returns website login page despite the fact that all the parameters are correct and I'm trying to access to a home page. I'm using the classic AuthScope.ANY method.
thanks for your help. Please don't pay attention to the url mistake and correct it easily if you wan't to give a try...



[NK] Please UseCodeTags
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

But when I go to that site it looks like it uses form-based authentication. That isn't supported by the authentication protocols in HttpClient. You're going to have to pass suitable parameters (i.e. whatever parameters the form asks for) in your POST request.
Alex Ter
Greenhorn

Joined: Sep 30, 2010
Posts: 10
well...... now I'm a bit embarassed cause I didn't read that httpclient did not handle this auth method... then that's really true that the website uses a post method so I just need to use something like :

HttpClient client = new HttpClient();
client.getParams().setParameter("id", "pass");



PostMethod method = new PostMethod("url");


?? thanks for the help
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, that's right. It might be a bit more complicated than that but you can work out the details as you go along.
Alex Ter
Greenhorn

Joined: Sep 30, 2010
Posts: 10
thanks so I tried this with no luck, still ending up on the login page:



[NK] Please UseCodeTags
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Where are you sending the user ID's value? Or the password? I don't see any of that.

Also, keep in mind that HTTP is essentially a stateless protocol. Cookies and sessions are a way around that, so you may want to use the cookies set by the original request in all following requests.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alex Ter
Greenhorn

Joined: Sep 30, 2010
Posts: 10
ok I confess that I'm quite new java and I thought I was sending the id by the writer.write(encodedData); so am I offending the java's god by this sentence ??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

That's sending the parameter name UserID, but no value, nor the password. You should send it as "UserID=xxx&password=yyy". But if you use HttpClient, you can use PostMethod and its addParameter methods.
Alex Ter
Greenhorn

Joined: Sep 30, 2010
Posts: 10
thanks for your answer. In fact, there is no need of any password so what i meant by userid in the code ex would be replaced in by the real userid but I guess I should use "UserID=xxx" right ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

It depends on how the user ID field is named on the website. It might be UserID, user, uid or anything similar. You'll have to check that out first.
Alex Ter
Greenhorn

Joined: Sep 30, 2010
Posts: 10
ok thanks, I tried to lo on another webpage (https) which has the same kind of form with a javascript validation. I used httpclient and get a message "other session running" as a response. Here is the code, it seems to go a bit further :

Alex Ter
Greenhorn

Joined: Sep 30, 2010
Posts: 10
any hint??... I'm quite stuck thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: httpclient athentification issue
 
Similar Threads
post html login
Java program to Login a website using url and to download a file
HttpClient- username and password error.
apache HttpClient make connection for url1 ,if url1 failed connect url2
HttpURLConnection and Redirects