java - Retrieve cookies from CookieStore set in request from java client application
问题描述
I have a java client that creates a request and sets the cookies in CookieStore before calling the rest service.
I am trying to set it on client and localContext (whichever works) this way
BasicClientCookie cookie = new BasicClientCookie("RSOMyLogin", "NDQ0NDQ0NDQ0");
cookie.setDomain("localhost");
cookie.setPath("/uploadFiles");
cookieStore.addCookie(cookie);
HttpPost httpRequest = new HttpPost(boxUploadURL);
CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute("RSOMyLogin", cookieStore);
HttpResponse httpResponse = client.execute(httpRequest, localContext);
System.out.println("response = "+httpResponse.toString());
It prints fine on console but with no cookies
response = HttpResponseProxy{HTTP/1.1 500 [Access-Control-Allow-Origin: *, Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, Access-Control-Allow-Credentials: true, Access-Control-Allow-Headers: authorization, content-type, x-auth-token, Cache-Control, remember-me, WWW-Authenticate, loggedinuser, Access-Control-Expose-Headers: xsrf-token, fileName, Content-Disposition, Content-Length, Content-Type, X-Content-Type-Options: nosniff, X-XSS-Protection: 1; mode=block, Cache-Control: no-cache, no-store, max-age=0, must-revalidate, Pragma: no-cache, Expires: 0, X-Frame-Options: DENY, X-Application-Context: application:local:9191, Content-Type: application/json;charset=UTF-8, Transfer-Encoding: chunked, Date: Tue, 27 Aug 2019 14:31:10 GMT, Connection: close] ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}}
On the servce side i am trying to fetch the cookies using the following code
private final String cookieName = "RSOMyLogin";
@RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
public Object handleFileUpload(HttpServletRequest request, @FormDataParam("files") MultipartFile[] files,
@FormDataParam("docType") String docType, @FormDataParam("fileMetadata") FileMetadata fileMetadata) {
HttpSession session = request.getSession(false);
String JSession = "";
Cookie[] cookie = request.getCookies();
System.out.println("cookie: "+cookie); //prints nothing
for (int i = 0; i < cookie.length; i++) { //java.lang.NullPointerException: null
if (cookie[i].getName() != null && cookie[i].getName().equalsIgnoreCase(cookieName)) {
JSession = cookie[i].getValue();
}
}
Is there anything i am missing while getting the cookies or am i not able to set them correctly?
Or am i completely off the tracks here :(
解决方案
在您的服务器代码中,您正在寻找带有 name 的 cookie RSOMyLogin
。为了让它工作,在您的客户端代码中使用上下文属性而不是 cookie 名称设置 cookie 存储:
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore)
它取代了旧的和不推荐使用的:
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore)
推荐阅读
- angular - 为什么组件与硬编码字符串的属性绑定不断出现未定义?
- raku - 可以对无符号变量或常量进行插值吗?
- java - 是否有与 python 的 dict.items()/dict.values()/dict.keys() 等效的 java?
- javascript - 我应该使用 Promise.All 而不是 async.each 吗?
- javascript - 可以溢出的元素:滚动气泡到文档吗?
- python - html文件未正确复制
- node.js - $match 在 mongodb 中查询不同的值
- php - PHP - Unicode 字符无法在控制台中正确显示
- angular - 如何以良好的编码实践方式修复此模块导入循环依赖?
- types - 是否可以将闭包分配给 impl Fn() 类型的变量?