简体   繁体   中英

C# httpwebrequest cookies

I'm using the below code to reuse the sessions.

Will I get the same session id on the grabUrl response cookies?

How do I know if both requests are using the same session/cookie?

Thanks.

CookieContainer cookCon = new CookieContainer();

Httpwebrequest loginReq = (httpwebrequest) loginReq.create(loginurl);

loginReq.cookiecontainer = cookCon;

... All the response stuffs

Httpwebrequest grabReq = (httpwebrequest) grabReq.create(grabUrl);

grabUrl.cookiecontainer = cookCon

When I add in below code to see the container contents, it shows that there is a session id:

        foreach (Cookie cook in cookieContainer.GetCookies(new Uri(loginurl)))
        {
            Console.WriteLine("Cookie_Get_Container:");
            Console.WriteLine("====================================================");
            Console.WriteLine("String: {0}", cook.ToString());
            Console.WriteLine("====================================================");
        }

So I added cookies to the login response:

cookieContainer.Add(new Uri(domainUrl), loginRes.Cookies);

I am not able to get the session id when I try to get from the cookieContainer.grabRes.Cookies. Any advise?

Since you are not showing code that grabs session cookie from first response I assume that you are not and the answer is "no, new session is created for second requset".

You want to get session ID (and other) from first response and set them on new requests (ie by adding them to the container).

Side note: depending on authenitcation method the information to authenticate the next requests (different from session ID) may not come in cookies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM