When using HttpWebRequest and HttpWebResponse in DotNet Compact Framework

Source: Internet
Author: User

When using HttpWebRequest and HttpWebResponse on Windows CE, The Listener does not recognize cookies. Each request creates a new Session on the server. For applications that require Session, this is unacceptable. I have been searching for it online for a long time. I found this article and provided a CookieManager class to manage cookies.

If you are the same as me, you do not have high requirements, but ensure that you can use the same session each time, you can use the following code:

Private static string cookies = string. Empty;

Private static void SaveCookie (HttpWebResponse response)
{
StringBuilder sb = new StringBuilder ();
For (var I = 0; I <response. Headers. Count; I ++)
{
If ("set-cookie". Equals (response. Headers. Keys [I]. ToLower ()))
{
Sb. Append (response. Headers [I] + ";");
}
}
String s = sb. ToString ();

If (s. IndexOf ("JSESSIONID")> = 0)
{
If (string. IsNullOrEmpty (cookies ))
{
Cookies = s;
}
Else
{
// Session Timeout, relogin
Cookies = s;
Relogin ();
}
}
}

Private static void SetCookie (HttpWebRequest request)
{
Request. Headers. Add ("Cookie:" + cookies );
}

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.