Java HttpClient cookie problem

I use Apache HttpClient to first request a page for cookies, and then send to a page with these cookies. To be able to get a second page, a cookie must be sent with a message. I read somewhere that HttpClient automatically saves and sends the necessary cookies, but for some reason I got stuck on the first page, possibly due to the fact that Cookies do not receive properly or are not sent properly.

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

}

So I hope someone can help me, thanks!

+4
source share
1 answer

You must specify how to manage these cookies:

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

+2
source

All Articles