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数据发布到我的服务器时使用它
答案 0 :(得分:0)
您是否已查看此链接
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) {
}