开发者

How does webservice maintain session when client is windows/Console app?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜