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 );
}