java HttpUrlConnection:Cookies显示在CookieStore中,但不显示在帖子标题中
在下面的代码中,我正在一个网站上运行post请求。我不明白的是,为什么cookie会通过cookiemanager显示,但它不会显示在帖子标题中。在代码中查看我的注释
有人能解释一下我错过了什么吗
CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cm);
...
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
OutputStream outputStream = connection.getOutputStream();
outputStream.write(urlParams.getBytes(charset));
// Clear cookies to prove they are not from an old request.
cm.getCookieStore().removeAll();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
throw new Exception("Invalid response code.");
// No cookie prints here:
Log.d("Aero", connection.getHeaderFields().toString());
List<HttpCookie> cookies = cm.getCookieStore().getCookies();
for (HttpCookie cookie : cookies) {
if (cookie.getName().equals("ASP.NET_SessionId")) {
// But we do get a cookie here
Log.d("Aero", cookie.toString());
}
}
共 (0) 个答案