0

In my android Application in the first screen user authenticates and servlet replies with a session id ... in the second screen the user again calls the servlet with the session id added in the url as ;jsession="sessionid" along with the parameters...

but the servlet is not get the session id and is responding as a new session without authentication...

where is the problem?

i am using this code for the connection

                    URL u = new URL(aurl[0]);
        url=aurl[0];
        publishProgress(""+20,"Connecting...");
        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        publishProgress(""+40,"Connecting...");
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();

        publishProgress(""+45,"Connecting...");
        publishProgress(""+40,aurl[0]);
        int lenghtOfFile = c.getContentLength();
        System.out.println(lenghtOfFile+"lenghtOfFile");

        is = c.getInputStream();
sunil shetty
  • 75
  • 1
  • 8

2 Answers2

2

I was facing the same problem and found a simple solution.

// First set the default cookie manager.

CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

// All the following subsequent URLConnections will use the same cookie manager.

URLConnection connection = new URL(url).openConnection();

// ...

connection = new URL(url).openConnection();

// ...

connection = new URL(url).openConnection();

// ...

This is the simplest solution I found. We need to set default CookieManager. Using java.net.URLConnection to fire and handle HTTP requests is the great blog.

Thanks

Community
  • 1
  • 1
Main Pal
  • 449
  • 5
  • 14
1

adding jseesioid in the url never worked .. the code worked using this solution

httpsessionmanagement

especially these two...

 // Gather all cookies on the first request.
URLConnection connection = new URL(url).openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...

// Then use the same cookies on all subsequent requests.
connection = new URL(url).openConnection();
for (String cookie : cookies) {
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
 }
 // ...
Community
  • 1
  • 1
sunil shetty
  • 75
  • 1
  • 8