C# - Cookie 管理
我早些时候在这里问了一个问题并得到了一些很棒的答复。从那时起,我一直在欺骗 Visual C#,并遇到了一些问题。
这里我制作了一个设置 cookie 的简单页面。
如果您访问它然后刷新,它会查看是否存在 cookie 并更改输出 html。
现在,我希望我的 C# 程序获取一个页面,获取一个 cookie,然后使用设置的 cookie 再次重新访问该页面,以便我的页面向我显示“已更新”消息。我通过以下方式完成了第一阶段:
private void button1_Click(object sender, RoutedEventArgs e)
{
WebRequest request = WebRequest.Create("http://www.binarywatch.biz/forms/cookietest.php");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
MessageBox.Show(responseFromServer, "Derp");
reader.Close();
dataStream.Close();
response.Close();
}
所以此时我有了页面 html,但我对如何获取 cookie 有点迷失(与 CookieContainer() 有关?),然后让页面知道我已经它(通过某种方式将其添加到 httpwebrequest 中?)
我当然尝试用谷歌搜索它,但我找到的很多答案都是关于 ASP.NET / web 编程的,而这不是我需要的。
附言。 WebRequest 和 HttpWebRequest 有什么区别?
我希望这不是太菜鸟,我有点困惑。
I asked a question on here earlier and got some fantastic responses. I've since been diddling Visual C# and ran into a bit of a problem.
Here I made a simple page that sets a cookie.
If you go to it and then refresh, it'll see if there's a cookie present and change the output html.
Now, I want my C# program to fetch a page, get a cookie and then re-visit the page again with the cookie that is set, so that my page presents me the "updated" message. I accomplished phase one via:
private void button1_Click(object sender, RoutedEventArgs e)
{
WebRequest request = WebRequest.Create("http://www.binarywatch.biz/forms/cookietest.php");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
MessageBox.Show(responseFromServer, "Derp");
reader.Close();
dataStream.Close();
response.Close();
}
So at this point I have the page html but I'm a bit lost as to how to go about getting a cookie (Something to do with CookieContainer() ? ) and then making the page know that I have it (by adding it to the httpwebrequest somehow?)
I tried googling it of course but a LOT of the answers I find are about ASP.NET / web programming and that's not what I need.
PS. What's the difference between WebRequest and HttpWebRequest?
I hope this isn't too noobish, I'm a bit stumped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 MSDN,在调用 getResponse 之前,您首先要创建 CookieContainer 的实例。之后,您应该能够从您创建的 CookieContainer 中获取 cookie 数据。
您应该能够重复使用相同的 CookieContainer 对象来确保服务器不断访问 cookie。
需要 GetCookies(domain),因为单个 CookieContainer 能够为多个域安全地存储单独的 cookie。
according to MSDN you'd first create an instance of the CookieContainer before calling getResponse. After that you should be able to get cookie data out of the CookieContainer you created.
You should be able to re-use the same CookieContainer object to make sure the server keeps getting access to the cookies.
The GetCookies(domain) is needed as a single CookieContainer is able to store separate cookies safely for multiple domains.
摘自“如何使用cookie在 httpwebrequest 中?”
是的,使用 CookieContainer。
从那个答案来看:
我还没有测试过这个,但它是公认的答案,所以它一定有效。希望它对你有用。
Taken from "how to use cookies in httpwebrequest?"
Yes, use CookieContainer.
From that answer:
I haven't tested this, but it was the accepted answer so it must work. Hope it works for you.