在我的android应用程序中,在第一个屏幕中,用户进行身份验证,servlet使用会话id进行回复。。。在第二个屏幕中,用户再次调用servlet,并在url中添加会话id作为;jsession="sessionid"以及参数。。。
但是servlet没有获取会话id,而是在没有身份验证的情况下作为新会话进行响应。。。
问题出在哪里?
我用这个代码连接
URL u = new URL(aurl[0]);
url=aurl[0];
publishProgress(""+20,"Connecting...");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
publishProgress(""+40,"Connecting...");
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
publishProgress(""+45,"Connecting...");
publishProgress(""+40,aurl[0]);
int lenghtOfFile = c.getContentLength();
System.out.println(lenghtOfFile+"lenghtOfFile");
is = c.getInputStream();
我遇到了同样的问题,找到了一个简单的解决方案。
//首先设置默认的cookie管理器。
CookieHandler.setDefault(新CookieManager(null,CookiePolicy.ACCEPT_ALL));
//以下所有后续URLConnection都将使用相同的cookie管理器。
URLConnection connection=新URL(URL).openConnection();
//。。。
connection=新URL(URL).openConnection();
//。。。
connection=新URL(URL).openConnection();
//。。。
这是我找到的最简单的解决方案。我们需要设置默认的CookieManager。使用java.net.URLConnection来激发和处理HTTP请求是一个很棒的博客。
感谢
在url中添加jseesioid从未奏效。。代码使用这个解决方案
http会话管理
尤其是这两个。。。
// Gather all cookies on the first request.
URLConnection connection = new URL(url).openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...
// Then use the same cookies on all subsequent requests.
connection = new URL(url).openConnection();
for (String cookie : cookies) {
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
// ...