Differences and connection between HttpWebRequest. CookieContainer and HttpWebResponse. Cookies

Source: Internet
Author: User

After a long time, I finally figured out the relationship between the two. I can't live up to my time in the morning ~~~

For more information about the nature of HttpWebRequest. CookieContainer and HttpWebResponse. Cookies, MSDN and many other blogs, I will not talk about them anymore. If you do not know about them, go and have a look.

Now let's talk about the differences between them. All the shoes that have used these two attributes have found that in most cases, the content of the two is the same. They should all store cookies related to this link, but they are a little different:

The Cookie contained in HttpWebRequest. CookieContainer is all Cookies related to this request. However, HttpWebResponse. Cookies only contain Cookies related to the URL of the last request in this request.

What about Shenma? The key isRedirectionThat is to say, if defa1.1.aspx is currently accessed and Default1.aspx generates a Cookie named "Default1" and redirects it to the Default2.aspx page. On the Default2.aspx page, a Cookie named "Default2" is generated and redirected to the Default3.aspx page. A "Default3" Cookie is generated on the defa3.3.aspx page. So far, the entire request has ended.

HttpWebRequest. CookieContainer contains three cookies: "Default1, Default2, and Default3". The GetCookies method is used.

At this time, HttpWebResponse. Cookies only contain one Cookie, that is, Default3. That is, the Cookie generated on the last page.

In this way, when you automatically submit a form to crawl the page that requires Session or Cookie persistence, You need to upload the newly generated HttpWebResponse. the Cookie in Cookies is saved and cannot directly overwrite the previous Cookie value. Compare the Cookie values in HttpWebRequest. CookieContainer and HttpWebResponse. Cookies of each request to prevent any omission and cause some page crawling failure.

 

PS: I just figured it out. Maybe I have a deep understanding of it. I also hope that I can beat the bricks O ~

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.