简体   繁体   English

C#httpwebrequest cookie

[英]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? 我将在grabUrl响应cookie上获得相同的会话ID吗?

How do I know if both requests are using the same session/cookie? 我如何知道两个请求是否使用相同的会话/ 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: 当我在下面的代码中添加以查看容器内容时,它表明存在一个会话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: 因此,我在登录响应中添加了cookie:

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. 当我尝试从cookieContainer.grabRes.Cookies获取时,我无法获取会话ID。 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". 由于您没有显示从第一次响应中获取会话cookie的代码,因此我认为您不是,并且答案为“否,将为第二个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). 您想从第一个响应中获取会话ID(及其他),并在新请求上进行设置(即通过将其添加到容器中)。

Side note: depending on authenitcation method the information to authenticate the next requests (different from session ID) may not come in cookies. 旁注:根据身份验证方法,用于身份验证下一个请求的信息(与会话ID不同)可能不会包含在cookie中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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