C# - Cookie 管理

发布于 2024-11-06 15:16:34 字数 1217 浏览 3 评论 0原文

我早些时候在这里问了一个问题并得到了一些很棒的答复。从那时起,我一直在欺骗 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-11-13 15:16:34

根据 MSDN,在调用 getResponse 之前,您首先要创建 CookieContainer 的实例。之后,您应该能够从您创建的 CookieContainer 中获取 cookie 数据。

(request as HttpWebRequest).CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
CookieCollection cookies = (request as HttpWebRequest).CookieContainer.GetCookies("www.binarywatch.biz");
string myValue cookies["myCookie"].Value

您应该能够重复使用相同的 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.

(request as HttpWebRequest).CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
CookieCollection cookies = (request as HttpWebRequest).CookieContainer.GetCookies("www.binarywatch.biz");
string myValue cookies["myCookie"].Value

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.

仙女 2024-11-13 15:16:34

摘自“如何使用cookie在 httpwebrequest 中?”

是的,使用 CookieContainer。

CookieContainer cookieContainer = new CookieContainer();
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
httpWebRequest.CookieContainer = cookieContainer;

从那个答案来看:

“在 GetResponse 调用之后
cookieContainer 将包含
请求者发回的 cookies
网址。”

我还没有测试过这个,但它是公认的答案,所以它一定有效。希望它对你有用。

Taken from "how to use cookies in httpwebrequest?"

Yes, use CookieContainer.

CookieContainer cookieContainer = new CookieContainer();
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
httpWebRequest.CookieContainer = cookieContainer;

From that answer:

"After the GetResponse call the
cookieContainer will contain the
cookies sent back from the requested
Url."

I haven't tested this, but it was the accepted answer so it must work. Hope it works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文