你能告诉我如何将 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);
编辑 - 进一步解释
我正在连接一位朋友编写的 servlet。我已登录并获得jsessionid
。现在我想发送另一个请求,需要传递 jsessionid 来进行授权。
Servlet 工作正常,因为我使用了 java HttpURLConnection,设置了 cookie,传递了它并且它工作了。现在,使用 HttpClient,我没有遇到任何异常,但朋友的 servlet 的返回代码表明请求中没有 sessionid。
另一个编辑 - 我有一个解决方案我设置了请求头的参数并且它起作用了。 Servlet 识别sessionid。
httppost.setHeader("Cookie", "JSESSIONID="+ getSessionId());
现在我的问题是:这个方法正确吗?
我很高兴能够解决这个问题:
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!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)