0
HttpHost targetHost = new HttpHost("myhost",8080, "http"); 
CredentialsProvider credsProvider = new BasicCredentialsProvider(); 
credsProvider.setCredentials(new
AuthScope(targetHost.getHostName(), targetHost.getPort()), new
UsernamePasswordCredentials("username", "password"));

// Create AuthCache instance 
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);

// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider); 
context.setAuthCache(authCache);

HttpGet httpget = new HttpGet("/"); 
try {

    HttpResponse response = httpclient.execute(targetHost, httpget, context);

    System.out.println(httpclient.getCookieStore().getCookies());

} catch (Exception e) {

} finally {
    try { 
        httpget.abort();}catch(Exception e){}
    }
}

but output i am getting is : [] nothing else . what mistake i am doing and how i can get jsessionId so that i can store it and use it later when i have to post json data to my server

cheb1k4
  • 2,316
  • 7
  • 26
  • 39
shrayansh
  • 15
  • 1
  • 8
  • i need jsessionId because i need it later when i will post my json data to the server – shrayansh Mar 24 '15 at 13:06
  • Your approach is right, it is strange that no cookies are storred. Maybe related to http://stackoverflow.com/questions/11331225/httpclient-not-storing-cookies-on-galaxy-s2 ? – Antoniossss Mar 25 '15 at 08:09

1 Answers1

0

Have you checked this link

How to manage sessions with Android Application

private void parseSessionID(HttpResponse response) {
    try {

        Header header = response.getFirstHeader("Set-Cookie");

        String value = header.getValue();
        if (value.contains("JSESSIONID")) {
            int index = value.indexOf("JSESSIONID=");

            int endIndex = value.indexOf(";", index);

            String sessionID = value.substring(
                    index + "JSESSIONID=".length(), endIndex);

            Logger.d(this, "id " + sessionID);

            if (sessionID != null) {
                classStaticVariable= sessionID;
            }

        }
    } catch (Exception e) {
    }
Community
  • 1
  • 1
Soham
  • 4,397
  • 11
  • 43
  • 71