C#通过httpwebrequest保持会话ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1453560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:42:02  来源:igfitidea点击:

C# keep session id over httpwebrequest

c#sessioncookieshttpwebrequest

提问by

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).

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

to make the other GET or POST requests:

发出其他 GET 或 POST 请求:

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.

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

回答by AnthonyWJones

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 的麻烦事。

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.

当响应设置 cookie 时,附加在请求中的 cookie 容器将接收并存储这些 cookie。因此,要在一系列请求之间维护相同的会话上下文,只需维护一个 cookie 容器实例并将其用于所有请求。

Your code becomes:-

您的代码变为:-

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

then:-

然后:-

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

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