Sending Cookie info in HttpRequest

前端 未结 5 1502
抹茶落季
抹茶落季 2020-12-10 20:11

I want to call a web service that requires an authentication cookie.

I have the cookie name and value. but I don\'t know how to inject the cookie in the request.

5条回答
  •  既然无缘
    2020-12-10 20:31

    Today, i solve the same problem using HttpUrlConnection with this:

            CookieManager cookieManager = CookieManager.getInstance();
            String cookieString = cookieManager.getCookie(SystemConstants.URL_COOKIE); 
            URL url = new URL(urlToServer);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Cookie", cookieString);
            connection.connect();
            OutputStream out = connection.getOutputStream();
            out.write(data.getBytes());
            out.flush();
            out.close();
    

提交回复
热议问题