• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Log in using Apache HttpComponents

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

I'm developing an app to do a POST log in on a server and access another page (who need logged status) to use those information.
The code:



I can submit the POST request (going through security certificates), but the servlet who redirects, tells me that i need enable cookies to log in.
Analyzing the returned headers from method, that API store the cookies in headers named "Set-Cookie", so i suppose that cookies are enabled.

Can anyone help me with that?
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Analyzing the returned headers from method, that API store the cookies in headers named "Set-Cookie", so i suppose that cookies are enabled.


That just means that the server sent some cookies back - which the client then needs to includes in the next request. My understanding was one needn't do anything for HC to support cookies, not even setting a CookieStore; this sample code would seem to bear that out. Make sure you're using the same HttpClient object for all requests; it keeps track of the cookies.
 
Pedro Ivo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the point.
I couldn't do another request; the first response isn't a logged page, it's a warning page about disabled cookies.
 
Ulf Dittmer
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That suggests that in normal web flow there is a page before this that sets the cookies which the login process expects. Try emulating that flow with HC.
 
Pedro Ivo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that you said, watching web flow through HttpFox when i log in, show the cookie JSESSIONID at the header "Cookie" instead "Set-Cookie" showed by API. That's maybe a problem? And if it's, is possible set API to save cookies in another header but "Set-Cookie"?
 
Saloon Keeper
Posts: 24315
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll skip my usual tirade about the fundamental insecurity of user-designed login servlets and their ilk, It's probably nothing you can control.

If you use the HttpURLConnection object to control the client/server conversation, that object will automatically transparently manage the cookies.

I don't know if that's an option, since you seem to be dipping into fine-tuning of the SSL configuration, which is something I don't normally do - I just let client and server negotiate automatically, just as they do in web browsers.
 
Pedro Ivo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show me a example where i do a post request, and then a request to another page (where i need logged status) to print their source code? I can't found a way to do without Apache API...
 
Pedro Ivo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:I don't know if that's an option, since you seem to be dipping into fine-tuning of the SSL configuration, which is something I don't normally do - I just let client and server negotiate automatically, just as they do in web browsers.



Don't worry about SSL configuration...just show me a simple way...
 
Ulf Dittmer
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My favored approach to programmatic web access is HtmlUnit rather than HttpComponents - the API is more full-featured. But HttpComponents should be up to the job.
 
Pedro Ivo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:My favored approach to programmatic web access is HtmlUnit rather than HttpComponents - the API is more full-featured. But HttpComponents should be up to the job.



Hi, thank's for your answer...i tried that request with HtmlUnit and had the same response from server...he redirect me to a page requesting enabled cookies.
But i read something about the "BROWSER_COMPATIBILITY" in "Cookies Policy" and i found that about change HtmlUnit Cookies Policy to "ACCEPT ALL COOKIES" instead "BROWSER COMPATIBILITY"...but i didn't understood how do this...

Can someone show me a step by step of how i can do this?

http://stackoverflow.com/questions/4681772/cookie-policy-in-htmlunit
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic