gpt4 book ai didi

Java HttpClient 关于 Cookie 的问题

转载 作者:搜寻专家 更新时间:2023-11-01 03:28:51 24 4
gpt4 key购买 nike

我使用 Apache HttpClient 首先请求 Cookie 的页面,然后发布到包含这些 Cookie 的页面。为了能够获得第二页,必须将 Cookie 与邮件一起发送。我在某处读到 HttpClient 自动保存并发送所需的 Cookie,但不知何故我一直停留在第一页,可能是由于 Cookie 未正确获取或未正确发送。

 public class Main {

static BufferedWriter writer;

public static void main(String args[]) throws ClientProtocolException, IOException {

getRequest();
}

public static void getRequest() throws ClientProtocolException, IOException {

HttpClient client = new DefaultHttpClient();

//the request to get the Cookies
HttpGet request = new HttpGet("http://www.SiteNameCutOut.cz");

List <NameValuePair> parameters = new ArrayList <NameValuePair>();
parameters.add(new BasicNameValuePair("view_state", "eaftOTAPef3NDs79"));
parameters.add(new BasicNameValuePair("age", "23"));
parameters.add(new BasicNameValuePair("button", "go"));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters);
HttpPost post = new HttpPost("http://www.SameSiteAsAbove.cz");
post.setEntity(entity);


//post.addHeader(request.getFirstHeader("Set-Cookie")); maybe?
post.addHeader("Host","theSiteHost");
post.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
post.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.addHeader("Accept-Language","en-us,en;q=0.5");
post.addHeader("Accept-Encoding","gzip, deflate");
post.addHeader("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
post.addHeader("Keep-Alive","115");
post.addHeader("Connection","keep-alive");


client.execute(request);

try {
request.abort();
HttpResponse response = client.execute(post);
writer = new BufferedWriter(new FileWriter("test001.html"));
writer.write(HttpHelper.request(response)); //gets html of the response
} catch (IOException ex) {
System.out.println("**Error**");
} finally {
if(writer != null){
writer.close();
}
else{
System.out.println("Writer is null");
}
}
}

所以我希望有人能帮助我,谢谢!

最佳答案

您应该指出如何管理这样的 cookie:

request.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

关于Java HttpClient 关于 Cookie 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5953628/

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