?
Solved

HttpURLConnection: Setting Cookies For Authentication

Posted on 2005-04-12
9
Medium Priority
?
6,672 Views
Last Modified: 2007-12-19
Hi,
I am trying to access a URL in my Java code that requires a login using the usual form box. As I am fimiliar with the website I know that all I need to capture for authentication is a 'jsessionid' variable and use this for future communication with the site.
How do I set this cookie value, I am trying:-
HttpURLConnection connection = (HttpURLConnection)testUrl.openConnection();
connection.setRequestProperty("Cookie","jsessionid=412364AA14H155125D125XSW1512");

However this will not work as I am trying to access the secure page, 'testUrl', before I have set the cookie value

Any ideas?
0
Comment
Question by:mahdlahd
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13760478
Try in CAPS:

HttpURLConnection connection = (HttpURLConnection)testUrl.openConnection();
connection.setRequestProperty("Cookie","JSESSIONID=412364AA14H155125D125XSW1512");

and remember it's only a temporary session cookie and so will only be valid for a short time.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13760552
Can't you just do:

1)  Read some sort of entry page.
2)  Get the requestProperty of Cookie, and store it
3)  Connect to the main page using this value...
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13760723
>> Can't you just do:

Yep, you should be able to use HttpUrlConnection to post to the login page and retrieve the cookie value by calling connection.getHeaderField("set-cookie");

http://forums.java.sun.com/thread.jspa?threadID=611517
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Expert Comment

by:TimYates
ID: 13760736
:-)
0
 

Author Comment

by:mahdlahd
ID: 13760807
>>Yep, you should be able to use HttpUrlConnection to post to the login page and retrieve the cookie value by calling connection.getHeaderField("set-cookie");


Ok could you explain this a little better, 'post to the login page' do you mean simulate a login and if so how is this done, I know using 'Get' you do it through the URL but Im not quite sure for 'Post'.
Once I do simulate a login I presume I retrieve the JSESSIONID value from the returned cookie and set this before opening subsequent connections? If so how is this done

Thanks again
0
 
LVL 3

Expert Comment

by:neonlines
ID: 13764423
I think "jsessionid" is not ur cookie. It's queryString parameter for the page

To send the Get/Post Parameters, or send cookies (in case of ur statement is true), see my posting in Q_21348112

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21348112.html

I think it'll clear all ur doubts

0
 
LVL 3

Expert Comment

by:neonlines
ID: 13764479
In that Thread, I explained about HttpsURLConnection, So, just ignore the SSL and related codings (Like LOADING CERTIFICATES , TrustManagerFactory, SSLContext) ...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13765133
>>  I think "jsessionid" is not ur cookie. It's queryString parameter for the page

no, jsessionid is a cookie id
0
 
LVL 3

Accepted Solution

by:
neonlines earned 375 total points
ID: 13769696
No Issues ... if it is Cookie, get the Cookies and save it in a variable. Remove the time stamp in that cookie and send it back

String cookie = connection.getHeaderField("set-cookie");

int j = cookie .IndexOf(";");
cookie = cookie.substring(0, j);
connection.setRequestProperty(cookie );




>> However this will not work as I am trying to access the secure page,

If it is Secure Page, U have to use HttpsURLConnection, not HttpURLConnection

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question