How does webservice maintain se开发者_如何学Cssion when client is windows/Console app?
Using cookies.
When you send HTTP requests, make sure to include a CookieContainer
. (assuming you're using HttpWebRequest
)
Under the covers, the C# WebClient is storing the cookie given to it by the web service.
Here is some sample code if someone is interested.
class Program
{
static void Main(string[] args)
{
CookieContainer session = new CookieContainer();
HttpWebRequest httpSomeRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8080/someURL");
httpSomeRequest.CookieContainer = session;
httpSomeRequest.GetResponse();
HttpWebRequest httpSomeOtherRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8080/someOtherURL");
httpSomeOtherRequest.CookieContainer = session;
httpSomeOtherRequest.GetResponse();
}
}
We just need to make sure that every HttpWebRequest
made, uses the same CookieContainer
instance.
精彩评论