【问题标题】:Retrieve cookie and send it in subsequent POST requests检索 cookie 并在后续的 POST 请求中发送
【发布时间】:2018-05-29 04:05:48
【问题描述】:

我想从网站读取两个数字(随机生成),然后使用它们来计算结果,然后使用 POST 请求提交结果。为此,我还需要提交该会话的 cookie,以便系统知道在该特定会话中产生的随机数。

为了阅读我正在使用的数字Jsoup

Document document = Jsoup.parse(Jsoup.connect("http://website.com/getNumbers").get().select("strong").text());
String[] numbers = document.text().split(" ");
String answer = methodThatComputesTheDesiredOutput(numbers);

现在我想发送一个包含该会话的answercookies 的POST 请求。这是一个部分实现的 POST 请求,仅包含一个参数 (answer):

HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://website.com/submitAnswer");
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("answer", answer);
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

如何在读取文档时获取cookie,然后将其作为POST请求的参数?

【问题讨论】:

    标签: java http cookies


    【解决方案1】:

    使用jsoup通过以下方式提取cookie:

        Response response = Jsoup.connect("http://website.com/getNumbers").execute();
        Map<String, String> cookies = response.cookies();
        Document document = Jsoup.parse(response.body());
    

    使用使用 jsoup 提取的 cookie 创建一个 BasicCookieStore。创建一个包含 cookie 存储的 HttpContext 并在执行下一个请求时传递它。

        BasicCookieStore cookieStore = new BasicCookieStore();
        for (Entry<String, String> cookieEntry : cookies.entrySet()) {
            BasicClientCookie cookie = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue());
            cookie.setDomain(".example.com");
            cookie.setPath("/");
            cookieStore.addCookie(cookie);
        }
    
    
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    
        HttpPost httpPost = new HttpPost("http://website.com/submitAnswer");
        List<NameValuePair> params = new ArrayList<NameValuePair>(1);
        params.add(new BasicNameValuePair("answer", answer);
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    
        httpClient.execute(httpPost, localContext);
    

    【讨论】:

    • 感谢您的回复。如何将参数answer 也传递给请求?
    • 和你已经做的一样。没有变化...我已经更新了答案以包含它。
    • 干杯。另一个问题:cookie.setDomain(".example.com"); cookie.setPath("/"); 我应该将什么值传递给这些方法?
    • 如果您拨打http://website.com/submitAnswer,请使用cookie.setDomain(".website.com"); cookie.setPath("/");
    【解决方案2】:

    发送您的第一个请求,如下所示:-

    Response res = Jsoup.connect("login Site URL")
            .method(Method.GET)
            .execute();
    

    现在获取 cookie 并使用 cookie 发送新请求,如下所示:-

     CookieStore cookieStore = new BasicCookieStore();
         for (String key : cookies.keySet()) {
            Cookie cookie = new Cookie(key, cookies.get(key));
            cookieStore.addCookie((org.apache.http.cookie.Cookie) cookie);
        }
           HttpClientContext context = HttpClientContext.create();
            context.setCookieStore(cookieStore);
            HttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost("http://website.com/submitAnswer");
            httpClient.execute(httpPost,context);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多