【问题标题】:how can i get jsessionId from client: DefaultHttpClient httpclient = new DefaultHttpClient();我如何从客户端获取 jsessionId: DefaultHttpClient httpclient = new DefaultHttpClient();
【发布时间】:2016-07-01 22:10:24
【问题描述】:
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){}
    }
}

但我得到的输出是:[] 没有别的。我在做什么错误以及如何获取 jsessionId 以便我可以存储它并在以后必须将 json 数据发布到我的服务器时使用它

【问题讨论】:

  • 我需要 jsessionId,因为我稍后将我的 json 数据发布到服务器时需要它
  • 你的做法是对的,奇怪的是没有存储cookies。可能与stackoverflow.com/questions/11331225/…有关?

标签: java android httpclient


【解决方案1】:

你检查过这个链接吗

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

【讨论】:

  • 但我的输出是 [ ] 空的。
  • 如果你能查看服务器日志会更好。可能是成功了,服务器没有给你任何响应。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 2011-07-05
  • 1970-01-01
相关资源
最近更新 更多