Java 在客户端请求上手动设置 Cookie: JSESSIONID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20066478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setting Cookie: JSESSIONID on client request manually
提问by Gazaz
im making a swing application which will sign in to a server; were im using HttpURLConnectionto submit my request and get my response.
我正在制作一个可以登录服务器的 Swing 应用程序;我正在使用HttpURLConnection提交我的请求并得到我的回复。
problem is whenthe httpRequest gets to the server the "Cookie: JSESSIONID" header is there, session id is there; but the request.getSession(false)will always return null.
问题是当httpRequest 到达服务器时,“Cookie: JSESSIONID”标头在那里,会话 ID 在那里;但request.getSession(false)将始终返回 null。
here is the code which i use to set the header on the client:
这是我用来在客户端上设置标头的代码:
connection.setRequestProperty("Cookie: JSESSIONID", client.getSessionId());
any help would be apprectiated
任何帮助都会得到认可
采纳答案by StanislavL
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);
See also this Java: How to make a HTTP browsing sessionand this Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request
另请参阅此Java:如何进行 HTTP 浏览会话和此Apache HttpClient 4.0.3 - 如何为 POST 请求设置带有 sessionID 的 cookie