HTTPclient Cookie Acquisition and setting

Source: Internet
Author: User

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:

    1. Defaulthttpclient httpclient=new defaulthttpclient();
    2. HttpResponse Response=httpclient. Execute(httppost);
    3. Cookiestore Cookiestore=httpclient. Getcookiestore();
    4. 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:

    1. Defaulthttpclient Httpclient2=new defaulthttpclient();
    2. Httpclient2. Setcookiestore(cookiestore);
    3. Add the cookie for the first request
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.