在 WebClient 中接受 Cookies? [英] Accept Cookies in WebClient?

查看:17
本文介绍了在 WebClient 中接受 Cookies?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始尝试 C# WebClient.我所拥有的是下面的代码,它从网站获取 html 代码并将其写入 .txt 文件.我遇到的唯一问题是某些网站要求您在使用该网站之前接受 cookie.这导致不是将真正的网站 html 代码写入 .txt 文件,而是写入 cookie 弹出 html 代码.

I just started experimenting with C# WebClient. What I have is the code below which gets html code from a website and writes it in a .txt file. The only problem I have is that some websites require you to accept cookies before you can use the website. What this causes is instead of writing the real website html code to the .txt file, it writes the cookie popup html code.

代码:

string downloadedString;
System.Net.WebClient client;

client = new System.Net.WebClient();
 
//"http://nl.wikipedia.org/wiki/Lijst_van_spelers_van_het_Nederlands_voetbalelftal"
downloadedString = client.DownloadString(textBox1.Text);

using (StreamWriter write = new StreamWriter("Data.txt"))
{
    write.Write(downloadedString);
}

那么有什么解决办法呢?有人可以指引我走向正确的道路吗?

So what is the solution to this? Can somebody direct me to the right path?

推荐答案

用法:

        CookieContainer cookieJar = new CookieContainer();
        cookieJar.Add(new Cookie("my_cookie", "cookie_value", "/", "mysite"));

        CookieAwareWebClient client = new CookieAwareWebClient(cookieJar);

        string response = client.DownloadString("http://example.com/response_with_cookie_only.php");


public class CookieAwareWebClient : WebClient
{
    public CookieContainer CookieContainer { get; set; }
    public Uri Uri { get; set; }

    public CookieAwareWebClient()
        : this(new CookieContainer())
    {
    }

    public CookieAwareWebClient(CookieContainer cookies)
    {
        this.CookieContainer = cookies;
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = this.CookieContainer;
        }
        HttpWebRequest httpRequest = (HttpWebRequest)request;
        httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        return httpRequest;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];

        //do something if needed to parse out the cookie.
        if (setCookieHeader != null)
        {
            Cookie cookie = new Cookie(); //create cookie
            this.CookieContainer.SetCookies(request.RequestUri, setCookieHeader);
        }
        
        return response;
    }
}

您将看到两个被覆盖的 GetWebRequest 和 GetWebResponse 方法.可以重写这些方法来处理 cookie 容器.

You will see two overridden methods for GetWebRequest and GetWebResponse. These methods can be overridden to handle the cookie container.

这篇关于在 WebClient 中接受 Cookies?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆