gpt4 book ai didi

java - 使用httpclient登录Xing

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:38 24 4
gpt4 key购买 nike

我在使用httpclient登录xing时遇到一些问题。当我登录手动捕获 livehttpheaders 的步骤并使用 addHeader("Cookie", "....") 重置 cookie 值时,它会起作用。但一旦我在浏览器中注销,它就不再起作用了。我假设我必须保存 cookie 并在每个帖子或获取方法中发送它们。我用 BasicCookieStore 尝试过,但并非所有 cookie 都被保存。当我运行这个时:

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

我得到:

Login form get: HTTP/1.1 200 OK
Initial set of cookies:
- [version: 0][name: c_][value: 0x11791BDA9E4611E49EF65B88668E3E29][domain: .xing.com][path: /][expiry: Fri Jan 12 14:40:50 CET 2035]
Login form get: HTTP/1.1 302 Found
Post logon cookies:
- [version: 0][name: _session_id][value: 335a78c6dca029dc47e1a7d6f86e2736][domain: login.xing.com][path: /][expiry: null]
- [version: 0][name: c_][value: 0x11791BDA9E4611E49EF65B88668E3E29][domain: .xing.com][path: /][expiry: Fri Jan 12 14:40:50 CET 2035]

但我认为缺少一些 cookie。当我手动登录时,会出现更多 cookie s_cc、s_sq、s_vi、s_fid 以及每次登录时都会不同的三个 cookie:login、xing 和 reg_ref现在我的问题是:我必须做什么才能获取这些 cookie 并在每种方法中正确发送它们?

最佳答案

您需要为httpclient设置cookiepolicy来处理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 的更多信息 here

关于java - 使用httpclient登录Xing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27999631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com