Link to home
Start Free TrialLog in
Avatar of benchpresser
benchpresser

asked on

cannot get set-cookie when connecting with java urlconnection on a ssl page for login

hi.
i want to login to a ssl page with an java application with username and password.
the page is https://secure...

the remote is asp.net application.

i tried urlconnection and httpurlconnection in my app. it fails logging.

when i login firefox+firebux i get the following:

Cache-Control      private
Content-Length      143
Content-Type      text/html; charset=utf-8
Date      Fri, 10 Feb 2012 01:18:22 GMT
Location      /data/community/index.aspx
Server      Microsoft-IIS/6.0
Set-Cookie      .ASPXAUTH=74D1555D330EFB9F8C59ACA42FA11AA83A04913CB9BC27B6966936CAFFB75EE9122ECC4D7E55DC4FC8DABD947A074250627B0E51245337FAE38D357149DFC5436FCECE7DEE158739BB2F4753B65DBC8A; path=/
X-AspNet-Version      1.1.4322
X-Powered-By      ASP.NET

but when i connect with my java app and get all response headers and cookies, i see all of them above except the set-cookie. why is the set-cookie not seen? because i do not have the set-cookie, i can not continue.

when i manually set the .ASPXAUTH to my request, i see the desired response. but of couse the .ASPXAUTH has a time interval and becomes invalid after some time.

when the .ASPXAUTH is set, then we have another set-cookie which starts: user-roles=...

how can i solve it?

- i also tried some httpsurlconnection, but did not enter into the detailed topic of keystore, certificates, x509, jsse.jar. do i have to use keytool and register the certificate to java environment?

- i tried setInstanceFollowRedirects to false which stated on some forums, should i try to manipulate some other flags?

- should i use cookie managers, handlers and set some request properties using the cookie stuff?

- on some forums it is said that java's url connection is very poor and works barely. it fails when using cookies in order to perform session-login acitivities. using apache http client is suggested. should i try that?
Avatar of for_yan
for_yan
Flag of United States of America image

didi you try  to login  using HttpClient ?

See description here:

http://hc.apache.org/httpclient-3.x/sslguide.html
Avatar of girionis
> but when i connect with my java app and get all response headers and cookies, i see all of them above except the set-cookie.

Are you setting the cookie? You need to set it in the request you are sending

String myCookie = ".ASPXAUTH=74D1555D330EFB9F8C59ACA42FA11AA83A04913CB9BC27B6966936CAFFB75EE9122ECC4D7E55DC4FC8DABD947A074250627B0E51245337FAE38D357149DFC5436FCECE7DEE158739BB2F4753B65DBC8A; path=/";
urlConnection.setRequestProperty("Cookie", myCookie);

Open in new window


For more information: http://www.hccp.org/java-net-cookie-how-to.html

> using apache http client is suggested. should i try that?

Yes, it's better than the standard java libraries. If you hit a wall then do use it, but try the standard Java classes first.
Avatar of benchpresser
benchpresser

ASKER

set-cookie is created dynamically on the remote side.

the steps are following:

request: (initial login screen, before sending userid and password)

Accept      text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding      gzip, deflate
Accept-Language      tr-tr,tr;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control      max-age=0
Connection      keep-alive
Host      secure......com
User-Agent      Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0

and response:

Cache-Control      private
Content-Length      12917
Content-Type      text/html; charset=utf-8
Date      Fri, 10 Feb 2012 09:04:39 GMT
Server      Microsoft-IIS/6.0
X-AspNet-Version      1.1.4322
X-Powered-By      ASP.NET

then i set user and password:

POST login.aspx?...

Accept      text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding      gzip, deflate
Accept-Language      tr-tr,tr;q=0.8,en-us;q=0.5,en;q=0.3
Connection      keep-alive
Host      secure.......com
Referer      https://secure.......com/Data/Community/Login.aspx?ReturnUrl=%2fData%2fcommunity%2fDataprices_daily_metals.aspx
User-Agent      Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0

response:

Cache-Control      private
Content-Length      161
Content-Type      text/html; charset=utf-8
Date      Fri, 10 Feb 2012 09:06:50 GMT
Location      /Data/community/Dataprices_daily_metals.aspx
Server      Microsoft-IIS/6.0
Set-Cookie      .ASPXAUTH=B4B1FE6EAD909BF33C7A7472FDDFFB80E4678C81B1BEA7BF8AC9ED215AF0A5869FAB0B9E1200B338921751A01753F67A4BFD49D88BABCD417E83816183056C25EC2DA0FDA1D4F3A153E7EC00D640B772; path=/
X-AspNet-Version      1.1.4322
X-Powered-By      ASP.NET

GET dataprices.aspx

Request

Accept      text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding      gzip, deflate
Accept-Language      tr-tr,tr;q=0.8,en-us;q=0.5,en;q=0.3
Connection      keep-alive
Cookie      .ASPXAUTH=B4B1FE6EAD909BF33C7A7472FDDFFB80E4678C81B1BEA7BF8AC9ED215AF0A5869FAB0B9E1200B338921751A01753F67A4BFD49D88BABCD417E83816183056C25EC2DA0FDA1D4F3A153E7EC00D640B772
Host      secure.......com
Referer      https://secure......com/Data/Community/Login.aspx?ReturnUrl=%2fData%2fcommunity%2fDataprices_daily_metals.aspx
User-Agent      Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0

Response:

Cache-Control      private
Content-Length      83262
Content-Type      text/html; charset=utf-8
Date      Fri, 10 Feb 2012 09:06:50 GMT
Server      Microsoft-IIS/6.0
Set-Cookie      user-roles=E5AD3596A1761795D6472DC1E8065CD011C4F316C729EE2AFBFBF5E95C00064AC17BB3A0003A551D2535886A4777F990422A8BFD2DEDA43541EF663F2E97C4A7D0252EFC095D8E2F03609239E22A4EA9F85C3886BBCA1CF6; expires=Fri, 10-Feb-2012 09:07:50 GMT; path=/
X-AspNet-Version      1.1.4322
X-Powered-By      ASP.NET
Well the only way to do it is to keep track of the cookies exchanged between the request/response and send them each time you request something. You said that it works if you set the .ASPXAUTH so it's just a matter of keeping track of what you send to the server. This is how the browsers work, they send the cookies each time with the request, essentially what you're trying to do is to emulate the behaviour of tghe browser.
there is some misunderstood.
if i would get the initial .APSXAUTH with my java app everythingq would work fine. But i can only get that using firebug. As I understood, the first ASPXAUTH will be send from the webserver. Then i use it for the next request. But i can not get the initial from my java app.
You are right, the initial .APSXAUTH will be sent from the web server upon successful login. Then your application should use the value of the APSXAUTH on every subsequent request to identity itself as the one that logged in initially. So the flow is

a) Your application sends the credentials (username/password) to the server
b) The server responds with the APSXAUTH cookie (if the login is successful)
c) You read the APSXAUTH value
d) You initiate a new request and include the APSXAUTH value as a cookie
e) On every subsequent request you must include the APSXAUTH value (until it expires).
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial