我使用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的多
发布评论