简体   繁体   English

我如何从客户端获取jsessionId:DefaultHttpClient httpclient = new DefaultHttpClient();

[英]how can i get jsessionId from client: DefaultHttpClient httpclient = new DefaultHttpClient();

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 我在做什么错误以及如何获取jsessionId以便我可以存储它并在以后将json数据发布到服务器时使用它

Have you checked this link 您是否检查过此链接

How to manage sessions with Android Application 如何使用Android应用程序管理会话

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) {
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM