C#通过httpwebrequest保持会话ID [英] C# keep session id over httpwebrequest

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

问题描述

在使用 C#.Net(如爬虫)浏览网站页面时,我需要保留相同的会话 ID.我找到了几种方法,一个 http 嗅探器非常方便,可以比较我的 IE 浏览器正在发送(HTTP 请求)和从 Web 服务器接收(HTTP 响应)的内容,因为重要信息在标头中(未显示)通过浏览器).请不要混淆从服务器到浏览器公开的会话 ID 和服务器代码私有的服务器会话变量(如 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()

我的请求是提供更好的代码或其他想法,以更好地保留爬虫会话.

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

推荐答案

如果您创建一个 cookie 容器并将其分配给您的第一个和第二个请求,您将不需要做所有关于从响应中复制 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.

您的代码变为:-

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#通过httpwebrequest保持会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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