How to Set multi-value Cookie when Winform uses HttpWebRequest

Source: Internet
Author: User
Tags dotnet

 

When sending multi-value cookies, you can use the following method to set multi-value cookies.

 

C # code

System. Net. HttpWebRequest r = System. Net. HttpWebRequest. Create ("http://www.mengxianhui.com: 8081/B. aspx") as System. Net. HttpWebRequest;

R. headers ["Cookie"] = "Url = http://dotnet.aspx.cc; UserInfo = UserName =" + System. web. httpUtility. urlEncode ("Meng xianhui") + "& UserId = net_lover ";

R. Proxy = null;

System. Net. HttpWebResponse response = r. GetResponse () as System. Net. HttpWebResponse;

This. textBox1.Text = new System. IO. StreamReader (response. GetResponseStream (). ReadToEnd ();

Response. Close ();

Or

 

C # code

System. Net. CookieCollection ccl = new System. Net. CookieCollection ();

System. Net. CookieContainer cc = new CookieContainer ();

System. net. cookie c = new Cookie ("UserInfo", "UserName =" + System. web. httpUtility. urlEncode ("Meng xianhui") + "& UserId = net_lover ");

C. Domain = "mengxianhui.com ";

Ccl. Add (c );

C = new Cookie ("Url", "http://dotnet.aspx.cc ");

C. Domain = "mengxianhui.com ";

Ccl. Add (c );

Cc. Add (ccl );

 

System. Net. HttpWebRequest r = System. Net. HttpWebRequest. Create ("http://www.mengxianhui.com: 8081/B. aspx") as System. Net. HttpWebRequest;

R. CookieContainer = cc;

R. Proxy = null;

System. Net. HttpWebResponse response = r. GetResponse () as System. Net. HttpWebResponse;

This. textBox1.Text = new System. IO. StreamReader (response. GetResponseStream (). ReadToEnd ();

Response. Close ();

 

 

Acceptor:

 

ASPX code

<% @ Page Language = "C #" AutoEventWireup = "true" %>

 

<Script runat = "server">

Protected void Page_Load (object sender, EventArgs e)

{

If (Request. Cookies ["UserInfo"]! = Null)

{

Response. Write ("Cookie:" + System. Environment. NewLine + System. Environment. NewLine );

HttpCookie aCookie = Request. Cookies ["UserInfo"];

Response. Write ("UserName =" + System. Web. HttpUtility. UrlDecode (aCookie ["UserName"]);

Response. Write (System. Environment. NewLine );

Response. Write ("UserId =" + aCookie ["UserId"]);

Response. Write (System. Environment. NewLine );

Response. Write ("Url =" + Request. Cookies ["Url"]. Value );

}

Else

{

Response. Write ("You have not set cookies ");

}

}

</Script>

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.