Java Apache HttpClient 4.0.3 - 如何为 POST 请求设置带有 sessionID 的 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4166129/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:13:39  来源:igfitidea点击:

Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request?

javapostcookiesapache-httpclient-4.x

提问by hitan

can you tell me how to store jsessionid in cookie, so it can be passed to the servlet with post request? I'm using Apache HttpClient version 4.0.3. All the solutions I've found explains how to do this with HttpClient 3.1. I've read tutorial and tried this, but it isn't working.

你能告诉我如何将 jsessionid 存储在 cookie 中,以便它可以通过 post 请求传递给 servlet 吗?我使用的是 Apache HttpClient 4.0.3 版。我找到的所有解决方案都解释了如何使用 HttpClient 3.1 做到这一点。我已经阅读了教程并尝试了这个,但它不起作用。

HttpPost httppost = new HttpPost(postData);
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", getSessionId());
cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
response = client.execute(httppost);

Edit - further explanations
I'm connecting to servlets written by friend. I've logged in and obtained jsessionid. Now I want to send another request and need to pass jsessionid for authorization purpose. Servlet works fine because I used java HttpURLConnection, set the cookie, passed it and it worked. Now with HttpClient I get no exceptions but the return code from friend's servlet indicates that there was no sessionid in the request.

编辑 - 进一步解释
我正在连接到朋友编写的 servlet。我已登录并获得jsessionid. 现在我想发送另一个请求并且需要传递 jsessionid 以进行授权。Servlet 工作正常,因为我使用了 java HttpURLConnection,设置了 cookie,传递了它并且它工作了。现在使用 HttpClient 我没有任何异常,但来自朋友的 servlet 的返回代码表明请求中没有 sessionid。

Another Edit - I've got one solutionI set the parameter of request header and it worked. Servlet recognized sessionid.
httppost.setHeader("Cookie", "JSESSIONID="+ getSessionId());

另一个编辑 - 我有一个解决方案,我设置了请求标头的参数并且它起作用了。Servlet 识别的 sessionid。
httppost.setHeader("Cookie", "JSESSIONID="+ getSessionId());

Now my question is: Is this method correct?

现在我的问题是:这种方法正确吗?

回答by khai

I did it by passing the cookie through the HttpContext:

我是通过 HttpContext 传递 cookie 来实现的:

HttpContext localContext = new BasicHttpContext();

localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

response = client.execute(httppost, localContext);

回答by Keith Lee

I am so glad to solve this problem:

我很高兴解决这个问题:

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); 

So Easy!

太简单!

回答by Ramdane Oualitsen

You should probably set all of the cookie properties not just the value of it. setPath(), setDomain()... etc

您可能应该设置所有 cookie 属性,而不仅仅是它的值。setPath(), setDomain()... 等

回答by liliya

HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
response = client.execute(httppost, localContext);

doesn't work in 4.5 version without

没有在 4.5 版本中不起作用

cookie.setDomain(".domain.com");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");