我正在制作一个将登录到服务器的swing应用程序;我正在使用 HttpURLConnection 提交我的请求并得到我的答复。
问题是当 httpRequest到服务器时,“Cookie:JSESSIONID”标头就在那里,会话ID就在那里;但 request.getSession(false)将始终返回null。
这是我用来在客户端设置标题的代码:
connection.setRequestProperty("Cookie: JSESSIONID", client.getSessionId());
任何帮助都会被提升
答案 0 :(得分:4)
HttpPost httppost = new HttpPost(postData);
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", getSessionId());
//cookie.setDomain("your domain");
cookie.setPath("/");
cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
response = client.execute(httppost);
另请参阅此Java: How to make a HTTP browsing session和此Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request