How to create a cookie using WebClient in C #

Source: Internet
Author: User

The cookie is not a limited only to Web browsers. Any http-aware client this supports cookie can deal with a cookie sending aSp. NET Web API. The following code example shows a class extended from WebClient. It overrides the virtual method getwebrequest to attach a instance of Cookiecontainer to the request. The Cookiecontainer object instance have to is reused across the requests to let it push cookie in the subsequent requests . For this reason, it is a class-level field and the same instance of the Web client are used to send multiple requests. Here we use a proxy of the address localhost and port 8888, that's Fiddler, to inspect requests and responses.

 Public classcookiewebclient:webclient {PrivateCookiecontainer jar =NewCookiecontainer (); protected OverrideWebRequest GetWebRequest (Uri address) {WebRequest request =Base.                     GetWebRequest (address); HttpWebRequest WebRequest = Request asHttpWebRequest;if(WebRequest! =NULL) Webrequest.cookiecontainer = jar;returnrequest; } } stringURL ="http://localhost:7077/api/employees/12345";


Chapter 4 http anatomy and SeCurity
70
Cookiewebclient client = new Cookiewebclient () {Proxy = new WebProxy ("localhost", 8888)//Fiddler}; Console.WriteLine (client. Downloadstring (URL)); Cookies are created here Console.WriteLine (client. Downloadstring (URL)); In this request, the cookie gets sent the Web API

How to create a cookie using WebClient in C #

Related Article

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.