Because the code and Java with Apache httpclient send post requests most of the repetition, so do not paste the entire code, only the different places posted.
Cookies must be sent before sending a cookie, so at least two requests are sent, and for the first time a cookie is used, and the second time a cookie is added before the request is sent
Before sending the POST request for the first time, a reference to the Defaulthttpclient object is established, and the reference is not established above, and is used directly after the new Defaulthttpclient object. Since it is necessary to send a cookie, it is necessary to obtain a cookie, which requires defaulthttpclient. After the first request is sent, you can use the Getcookiestore () of the Defaulthttpclient object. To get a Cookiestore object, the cookie we use exists here. Or just post these lines of code:
The above 37 lines make the following modifications:
- Defaulthttpclient httpclient=new defaulthttpclient();
- HttpResponse Response=httpclient. Execute(httppost);
- Cookiestore Cookiestore=httpclient. Getcookiestore();
- Get cookies
Second request, copy the code of the first request again. Of course, the variable name will be repeated, you can change it. Now you want to add the cookie you just received before sending the request, or change the 37 lines above:
- Defaulthttpclient Httpclient2=new defaulthttpclient();
- Httpclient2. Setcookiestore(cookiestore);
- Add the cookie for the first request
- HttpResponse response2=httpclient2. Execute(httppost2);
© all, frozen fish. Please respect the author's labor results, copy reproduced and retain the site link! Application Development Notes
For more HTTP client applications, see the following connections:
http://renjie120.iteye.com/blog/1727933
HTTPclient Cookie Acquisition and setting