【问题标题】:Retrieve cookies from CookieStore set in request from java client application从 Java 客户端应用程序的请求中设置的 CookieStore 中检索 cookie
【发布时间】:2019-08-27 14:49:36
【问题描述】:

我有一个 java 客户端,它在调用其余服务之前创建一个请求并在 CookieStore 中设置 cookie。

我正在尝试以这种方式在客户端和 localContext 上设置它

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

在控制台上打印正常,但没有 cookie

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]}}

在服务端,我正在尝试使用以下代码获取 cookie

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

在获取 cookie 时有什么遗漏或者我无法正确设置它们吗?

或者我在这里完全偏离轨道:(

【问题讨论】:

    标签: java rest spring-boot cookies httpclient


    【解决方案1】:

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

    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore)
    

    替换旧的和不推荐使用的:

    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore)
    

    【讨论】:

    • 仍然出现空异常。我还需要从服务方面进行更改吗? ClientContext.COOKIE_STORE 也被弃用了。
    • 我添加了在本地上下文中设置 cookie 存储的新方法
    • 即使给出了空指针,我认为如果我们将其存储在 cookieStore 中,还有其他方法可以查看 cookie(request.getCookies() 是麻烦)
    猜你喜欢
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 2012-03-02
    • 2013-03-10
    • 1970-01-01
    • 2014-01-04
    相关资源
    最近更新 更多