C#中保持会话ID HttpWebRequest的多 [英] C# keep session id over httpwebrequest

查看:512
本文介绍了C#中保持会话ID HttpWebRequest的多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#.NET(如履带)在导航网站的页面时需要preserve相同的会话ID。我发现一对夫妇的方法,一个HTTP嗅探器很方便,比较我的IE浏览器中发送内容(HTTP请求)和Web服务器(HTTP响应)接收,作为重要的信息是在标题(即不显示由浏览器)。
请不要让会话ID之间的混淆是公共的从服务器到浏览器和服务器的会话变量,这是私人服务器code(如PHP)。

I need to preserve the same session id when navigating over a site's pages using C#.Net (like a crawler). I found a couple of methods, a http sniffer was very handy, to compare what my IE browser was sending (HTTP request) and receiving from the web server (HTTP response), as the important information is in the headers (that are not displayed by the browser). Please don't make confusion between session id which is public from server to browser, and server's session variables which are private to server code (like php).

WebHeaderCollection headerCollection = new WebHeaderCollection();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
  /* save headers */
  for (int i = 0; i < response.Headers.Count; i++)
  {
     headerCollection.Add(response.Headers.AllKeys[i], response.Headers.Get(i));
  }
  /* save cookies */
  cookieContainer = new CookieContainer();
  foreach (Cookie cookie in response.Cookies)
  {
    cookieContainer.Add(cookie);
  }
}

让其他GET或POST请求:

to make the other GET or POST requests:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
...
/* restore PHPSESSID */
for (int i = 0; i < headerCollection.Count; i++)
{
 string key = headerCollection.GetKey(i);
 if (key == "Set-Cookie")
 {
  key = "Cookie";
 }
 else
 {
  continue;
 }
 string value = headerCollection.Get(i);
 request.Headers.Add(key, value);
}
/* restore cookies */
request.CookieContainer = cookieContainer;
/* complete request */
Stream writeStream = request.GetRequestStream()

我的要求是更好的code或更多的想法,有助于做出更好的履带式会话preserving。

My request is to contribute with better code, or additional ideas to make a better crawler session preserving.

推荐答案

如果您创建一个cookie的容器,并分配到这两个第一和第二个请求,你将不再需要做的一切,混日子从响应复制饼干

If you create a single cookie container and assign that to both your first and second request you won't need to do all that mucking about copying cookies from the response.

在Cookie由一个响应设置连接请求将接收并存储这些Cookie cookie的容器。因此,要维持了一系列的要求之间的同一个会话的上下文只是保持一个cookie容器实例,并使用所有的请求。

When cookies are set by a response the cookie container that is attached the request will receive and store those cookies. So to maintain the same session context between a series of request just maintain a single cookie container instance and use that with all the requests.

您code变为: -

Your code becomes:-

cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
  // Do stuff with response
}

那么: -

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
...

request.CookieContainer = cookieContainer;
Stream writeStream = request.GetRequestStream()

这篇关于C#中保持会话ID HttpWebRequest的多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆