Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用httpclient登录到Xing_Java_Httpclient_Session Cookies_Xing - Fatal编程技术网

Java 使用httpclient登录到Xing

Java 使用httpclient登录到Xing,java,httpclient,session-cookies,xing,Java,Httpclient,Session Cookies,Xing,我在使用httpclient登录xing时遇到一些问题。 当我使用livehttpheaders手动登录捕获步骤并使用addHeader(“cookie”、“…”)重置cookie值时,它会起作用。 但一旦我在浏览器中注销,它就不再工作了。 我假设我必须保存cookies并在每个post或get方法中发送它们。 我在BasicCookies商店试过,但并不是所有的饼干都保存了下来。 当我运行此命令时: BasicCookieStore cookieStore = new BasicCookieS

我在使用httpclient登录xing时遇到一些问题。 当我使用livehttpheaders手动登录捕获步骤并使用
addHeader(“cookie”、“…”)
重置cookie值时,它会起作用。 但一旦我在浏览器中注销,它就不再工作了。 我假设我必须保存cookies并在每个post或get方法中发送它们。 我在BasicCookies商店试过,但并不是所有的饼干都保存了下来。 当我运行此命令时:

BasicCookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCookieStore(cookieStore)
            .build();
try {
     HttpGet httpget = new HttpGet("https://www.xing.com/");
     CloseableHttpResponse response = httpclient.execute(httpget, httpContext);
     try {
         HttpEntity entity = response.getEntity();

         System.out.println("Login form get: " + response.getStatusLine());
         EntityUtils.consume(entity);

         System.out.println("Initial set of cookies:");
         List<Cookie> cookies = cookieStore.getCookies();
         if (cookies.isEmpty()) {
             System.out.println("None");
         } else {
             for (int i = 0; i < cookies.size(); i++) {
                 System.out.println("- " + cookies.get(i).toString());
             }
         }
     } finally {
         response.close();
     }
     HttpPost httpPost = new HttpPost("https://login.xing.com/login");
     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
     nvps.add(new BasicNameValuePair("username", "name%40host.com"));
     nvps.add(new BasicNameValuePair("password", "mypassword"));
     httpPost.setEntity(new UrlEncodedFormEntity(nvps));
     httpPost.addHeader("Referer", "https://www.xing.com/");
     httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");

     CloseableHttpResponse response1 = httpclient.execute(httpPost, httpContext);
     try {
         HttpEntity entity = response1.getEntity();
         System.out.println("Login form get: " + response1.getStatusLine());
         EntityUtils.consume(entity);

         System.out.println("Post logon cookies:");
         List<Cookie> cookies = cookieStore.getCookies();
         if (cookies.isEmpty()) {
             System.out.println("None");
         } else {
             for (int i = 0; i < cookies.size(); i++) {
                 System.out.println("- " + cookies.get(i).toString());
             }
         }
     } finally {
         response1.close();
     }
但我想有些饼干不见了。当我手动登录时,会有更多的cookies
s_cc、s_sq、s_vi、s_fid
,并且每次登录时都会有三个不同的cookies:
login、xing和reg_ref

现在我的问题是:我必须做什么才能获得这些cookie并在每种方法中正确发送它们?

您需要为httpclient设置cookie策略以处理cookie,而且cookie存储为空的原因可能违反了给定的cookie策略,因此您必须找到最适合的cookie策略

Apache HttpClient 3.+示例

httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
Apache HttpClient 4.3+示例

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

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

有关Apache HttpClient cookiepolicy的更多信息

感谢您的回答,但更改cookiepolicy对我来说不起作用。通过
Browser\u兼容性
Best\u匹配
我收到了与上面发布的相同的cookie。使用
Netscape
Standard
未找到任何cookie。我如何测试
RFC_2109
,因为它没有出现在我的列表中?我的代码中还有其他问题吗?
  RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
CookieStore cookieStore = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);

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