【发布时间】: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