2009-12-28 29 views
1

我花了一些时间研究这个,仍然无法弄清楚。它看起来很简单,所以我觉得自己像个白痴一样问这个问题,但经过一段时间的研究后,我似乎无法得到它的诀窍。C#.NET Cookie处理后登录

我需要以编程方式登录到此站点:https://wholesale.frontiercoop.com/,存储来自登录名的cookie,并在下次登录时重新提交。它看起来像是一个POST提交(我使用Firefox上的Firebug收集的),所以我想出了如何存储我认为的cookie。我只是无法弄清楚如何在下次调用网站时提交它,所以它不会自动将我重定向到登录页面。这是否仅仅是Web浏览器对象调用的一个参数?

感谢您的帮助。

回答

2

在.NET你必须使用的CookieContainer,如:

HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("https://wholesale.frontiercoop.com/"); 
req.Method = "POST"; 
CookieContainer container = new CookieContainer(); 
req.CookieContainer = container; 
// Write the POST data and get the request... 
... 
// ...and once the request is done, the cookie is in 'container'. 
// Then, for subsequent requests you set the CookieContainer of the request to the one above 
otherRequest.CookieContainer = container; 
+0

是否有可能使用WebBrowser对象执行此操作?我正在尝试将HTML源代码从网站中提取出来,并且需要使用WB来访问此网站。感谢您的答案,顺便说一句。 – 2010-01-04 19:35:23

1

当您使用WebRequest对象的下一个请求,您需要分配有你返回的cookie在它的CookieContainer。

example显示如何使用Hotmail,它应该告诉你你需要的大部分。