Cookie的Java HttpClient问题

Cookie的Java HttpClient问题,第1张

概述我使用Apache HttpClient首先请求Cookie的页面,然后发布到包含这些Cookie的页面.为了能够获得第二页,必须使用帖子发送Cookie.我已经读过HttpClient自动保存的地方并发送所需的Cookie,但不知怎的,我一直卡在第一页,可能是因为Cookies没有正常使用,或者没有正确发送. public class Main {static BufferedWriter 我使用Apache httpClIEnt首先请求cookie的页面,然后发布到包含这些cookie的页面.为了能够获得第二页,必须使用帖子发送cookie.我已经读过httpClIEnt自动保存的地方并发送所需的cookie,但不知怎的,我一直卡在第一页,可能是因为cookies没有正常使用,或者没有正确发送.

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

总结

以上是内存溢出为你收集整理的Cookie的Java HttpClient问题全部内容,希望文章能够帮你解决Cookie的Java HttpClient问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1058641.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存