Tag: httpwebrequest cookiecontainer

CookieContainer混乱

据我所知,CookieContainer通过HttpWebRequests持久化Cookie的基本用法如下: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); CookieContainer cookies = new CookieContainer(); request.CookieContainer = cookies; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { // Do stuff with response } 然后: request = (HttpWebRequest)WebRequest.Create(new url); request.CookieContainer = cookies; etc… 但我无法理解这个过程背后的逻辑。 变量cookie在初始化后似乎没有被重新分配。 第一个WebResponse的cookie到底是如何进入第二个WebRequest的?

在winforms中使用HttpWebRequest传递cookie?

请参阅以下代码: objCookieContainer = new CookieContainer(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“http://website.com/login.php?user=xxx&pass=xxx”); request.Method = WebRequestMethods.Http.Get; request.Timeout = 15000; request.Proxy = null; request.CookieContainer = objCookieContainer; HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(“http://website.com/page.php?link=url”); newRequest.Method = WebRequestMethods.Http.Get; newRequest.Timeout = 15000; newRequest.Proxy = null; newRequest.CookieContainer = objCookieContainer; HttpWebResponse response = null; response = (HttpWebResponse)request.GetResponse(); string readerRequest = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd(); response = (HttpWebResponse)newRequest.GetResponse(); string readerNewRequest […]

CookieContainer处理路径(谁吃了我的cookie?)

我正在开发一个涉及一些基本网络爬行的项目。 我一直在成功使用HttpWebRequest和HttpWebResponse。 对于cookie处理,我只有一个CookieContainer,我每次都分配给HttpWebRequest.CookieContainer。 每次我自动填充新的cookie,不需要我的额外处理。 直到不久之前,当其中一个曾经工作的网站突然停止工作时,这一切都很好。 我有理由相信这是一个有问题的cookie,但是我没有记录过以前的cookie,所以我不是百分百肯定的。 我用以下代码设法模拟了这个问题: CookieContainer cookieJar = new CookieContainer(); Uri uri1 = new Uri(“http://www.somedomain.com/some/path/page1.html”); CookieCollection cookies1 = new CookieCollection(); cookies1.Add(new Cookie(“NoPathCookie”, “Page1Value”)); cookies1.Add(new Cookie(“CookieWithPath”, “Page1Value”, “/some/path/”)); Uri uri2 = new Uri(“http://www.somedomain.com/some/path/page2.html”); CookieCollection cookies2 = new CookieCollection(); cookies2.Add(new Cookie(“NoPathCookie”, “Page2Value”)); cookies2.Add(new Cookie(“CookieWithPath”, “Page2Value”, “/some/path/”)); Uri uri3 = new Uri(“http://www.somedomain.com/some/path/page3.html”); // Add the cookies […]

C#:将CookieContainer写入磁盘并重新加载以供使用

我有一个名为CookieJar的HttpWebRequest / HttpWebResponse会话中提取的CookieContainer 。 我希望我的应用程序在运行之间存储cookie,因此在一次运行程序中在CookieContainer收集的cookie也将在下一次运行时使用。 我认为这样做的方法是以某种方式将CookieContainer的内容写入磁盘。 我的问题是: 如何将CookieContainer写入磁盘 ? 是否有内置function,或者,如果没有,人们采取了哪些方法? 有没有可用于简化此类的课程? 一旦你将CookieContainer写入磁盘, 你如何重新加载它 ? 更新:第一个答案建议CookieContainer 序列化 。 但是,我不太熟悉如何序列化和反序列化这些复杂的对象。 你能提供一些示例代码吗? 建议是使用SOAPFormatter 。