Cookie的Java HttpClient问题

我使用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);

点赞