首页 > 解决方案 > 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 :(

标签: javarestspring-bootcookieshttpclient

解决方案


在您的服务器代码中,您正在寻找带有 name 的 cookie RSOMyLogin。为了让它工作,在您的客户端代码中使用上下文属性而不是 cookie 名称设置 cookie 存储:

localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore)

它取代了旧的和不推荐使用的:

localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore)

推荐阅读