• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not recognizing session in HttpURLConnection  RSS feed

 
Chris Garrison
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all: I've done several searches on this subject. I've combined a lot of the info to get where I am, but it isn't quite working.
I'm trying to utilize the HttpURLConnection to pull the output from a web application and display in another (specifically in a portal). I log in (manually) with the first "click" but after that, I always get the Session Timeout message.
I know it's probably something silly, but it's my first time.
What am I doing wrong? Heres the code.
String readLine="";
StringBuffer contHTML=new StringBuffer();
String htmlString = null;
try{
URL urltt = new URL(myURL);
HttpURLConnection connection = (HttpURLConnection) urltt.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setAllowUserInteraction(true);
connection.setFollowRedirects(false);

String cookee = theBean.getCookee();
if (cookee != null){
connection.setRequestProperty("set-cookee", cookee);
}else{
cookee = connection.getHeaderField("set-cookee");
theBean.setCookee(cookee);
}

connection.connect();
BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((readLine=br.readLine()) != null) {
contHTML.append(readLine);}
htmlString = contHTML.toString();

}catch(Exception ex){System.out.println("problem -- " + ex);
}

String outputString = htmlString.replaceAll("XXXXXXXXXX", "YYYYYYYYY");// i'm rewriting portion of the url's here
out.print(outputString);


Thanks for you help!
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This piece of code seems questionable.
Assuming you want to send a cookie back to the server (who would have issued your web browser a "Set-Cookie" header, use this instead:

The HTTP repsonse header (ie from the server) contains the "Set-Cookie" directive, the subsequent HTTP request (ie from the client) should just contain a "Cookie" directive.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note the correct spelling of "cookie" -- it matter when you're talking about HTTP headers!
 
Chris Garrison
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean to have the following (from the suspect section of code):
if (cookie != null){
connection.setRequestProperty("Cookie", cookie);
}else{
cookie = connection.getHeaderField("set-cookie");
theBean.setCookie(cookie);}
or should I have an if statement at all?
I put this in my code and it seemed to work (sort of) for a second. The session was maintained . Once I restarted Tomcat, it stopped working.
The cookie is a JSESSIONID. I think I may not be using the correct call to get the correct (initial) Cookie from the server - or do I even need to do this?
Thanks for your patience. Sorry Im a litle dense.
And thanks for the heads up on the cookie spelling - javaranch gave me errors when I tried to spell cookie
[ September 03, 2003: Message edited by: Chris Garrison11 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look at the HttpClient package - its part of the "commons" project at jakarta.apache.org/commons
It lets you simulate cookies and other browser functions.
Bill
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!