How does a webservice maintain a session when the client is a window / console application?

How does a webservice maintain a session when the client is a window / console application?

+3
source share
3 answers

Use of cookies.

When sending HTTP requests, be sure to enable it CookieContainer. (assuming you are using HttpWebRequest)

+2
source

Under covers, the C # web client stores a cookie provided by the web service to it.

+2
source

Here is a sample code if anyone 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 created one HttpWebRequestuses the same instance CookieContainer.

0
source

Source: https://habr.com/ru/post/1774331/


All Articles