Java 我应该使用apache HttpClient重新生成cookies CookieStore吗?

Java 我应该使用apache HttpClient重新生成cookies CookieStore吗?,java,apache,apache-httpclient-4.x,Java,Apache,Apache Httpclient 4.x,我需要登录到外部网站,并将HttpClient与CookieStore结合使用。作为向导,我使用 这是我的密码: RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build(); CookieStore cookieStore = new BasicCookieStore(); HttpClientContext context = Http

我需要登录到外部网站,并将HttpClient与CookieStore结合使用。作为向导,我使用

这是我的密码:

    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
    CookieStore cookieStore = new BasicCookieStore();
    HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(cookieStore);

    CloseableHttpClient httpClient = HttpClients.custom()
            .setDefaultRequestConfig(globalConfig)
            .setDefaultCookieStore(cookieStore)
            .build();

    HttpGet httpGet = new HttpGet(urlAddress);

    httpGet.addHeader("Accept", "text/html, application/xhtml+xml, */*");
    httpGet.addHeader("Accept-Language", "en-US,en;q=0.7,ru;q=0.3");
    httpGet.addHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
    httpGet.addHeader("Accept-Encoding","gzip, deflate");
    //httpGet.addHeader("Host", host); // should I copy host here?
    httpGet.addHeader("Connection", "Keep-Alive");

    CloseableHttpResponse response1 = httpClient.execute(httpGet);

    List<Cookie> cookies1 = context.getCookieStore().getCookies();

    try {
        System.out.println(response1.getStatusLine());
        HttpEntity entity1 = response1.getEntity();
        // do something useful with the response body
        streamToFile(entity1.getContent(), "./login_result_1.html");

        // and ensure it is fully consumed
        EntityUtils.consume(entity1);
    } finally {
        response1.close();
    }
那很好

当我向网站提出进一步请求时,我是否应该使用任何特殊代码来传递在上述请求中获得的cookie

我应该重新生成cookie还是由Apache完成

我找不到明确的答案。另外,我的进一步请求没有被网站正确处理,它不允许我登录

    HttpPost httpPost = new HttpPost(urlAddress);
    httpPost.addHeader("Accept", "text/html, application/xhtml+xml, */*");
    httpPost.addHeader("Accept-Language", "en-US,en;q=0.7,ru;q=0.3");
    httpPost.addHeader("Referer", "http://virtonomica.ru/");
    httpPost.addHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
    httpPost.addHeader("Accept-Encoding","gzip, deflate");

    httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
    httpPost.addHeader("Accept-Encoding", "gzip, deflate");
    //httpPost.addHeader("Host", "virtonomica.ru");
    //httpPost.addHeader("Content-Length", "51");//LENGTH
    httpPost.addHeader("Connection", "Keep-Alive");
    httpPost.addHeader("Cache-Control", "no-cache");
    httpPost.setEntity(new UrlEncodedFormEntity(nvps)); // nvps is List<NameValuePair> which contains POST data

    CloseableHttpResponse response = httpClient.execute(httpPost);

请告知。

您使用的是同一个cookieStore参考吗?上面的代码是在一个函数原型中编写的,因此是的,cookieStore是相同的。事实上,我使用的是同一个httpClient实例,因此我假设CookieStore也被重用。