A question about httpclient cookies

Source: Internet
Author: User

Previously, I used httpclient to capture webpages and analyze webpage data. Sometimes I used httpclient to automatically log on to webpages.GetDo not analyze or retrieve cookies in the browser. However, at the beginning, people who use httpclient may have a problem:Some cookies we see in the browser use ourProgramBut it cannot be obtained. Why?.

I have been troubled by this problem before, and I have made some comments on some posts of csdn. This is also a story later. Many of my friends sent me emails asking me some cookie questions about httpclient. However, because I don't often check my mailbox, some emails have not been found for a long time, sorry. Today is just a little time, so I wrote this articleArticleFirst, I would like to summarize the knowledge I have learned so that I will not forget it. Second, I would like to give an explanation to my friends and hope to help my friends who have found this article. Thank you for your support.

First, I want to note that httpclient simulates a browser to send a request and then waits for a response. In this caseYesHttpclient can obtain the cookie sent back and written by the server. There is nothing to understand. This problem is clear. httpclient simulates the browser to send requests, but sometimes the browser automatically generates cookies. httpclient does not automatically generate cookies, so we cannot have some cookies. Unless simulated by yourself. Like this image,
In response, we see the set-Cookie, which is the cookie written back by the server. We can certainly get it.. I can say that httpclient can have the same cookie as a browser, but some people have started to disagree: According to your statement, why can't I still get many cookies? This is a more detailed question.

As mentioned above, some cookies are clearly not in the browser, but I still cannot get them. Why? Let's take an example of automatic login. I sent the user name and password to the verified URL, but it's not a problem. Why.The reason may be that when we request a login page, the server has written some cookies back to the browser. Then we should first request the rul on the login page and extract the obtained cookie to set it into the authentication username request. As long as the cookie is complete, the problem may be solved. That is to say, different requests may have different cookies, and httpclient does not have the automatic Redirect function (we need to manually redirect based on the response status code ), therefore, if we want to obtain these cookies, we have to find the URL to be requested. Here, I will teach you how to clear all the cookies in the browser first, and then check that the cookie in the request URL contains a write-back cookie, and then send the request.

This is a skill, and the trouble is necessary. For example, for some websites with high security, some cookies request an image and write it back, which is troublesome to find out, but there is no way to do it !! Another kind of cookie is generated in JS. What should we do if some brothers make a mistake.What else can I do? Take the JS file and run it with the JS engine in Java JDK..In this case, any cookies cannot be obtained.

Note that it is best to use a singleton instance when using an httpclient instance.,In addition, httpclient does not have the cookie storage function (after a request, we do not seem to have a manual jump based on the response code). We should pay attention to setcookie and then request. Otherwise, the problem is unknown. The problem is analyzed in detail.

After changing the company, I haven't used httpclient for a long time. I don't know if the problem is clear.Supplement. Email: guanqing527@163.com, but not often check, reply will be a bit late, everybodySeaHan. The above is my personal opinion and may not solve your problems. You are welcome to criticize and correct me. In fact, I am also a newcomer, Mr. Huang Mao, who specializes in academics. Thank you for your support!

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.