c# - 在 WebClient 中接受 Cookies?

标签 c# html cookies webclient

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

代码:

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);
}

那么解决这个问题的方法是什么?有人可以指引我走上正确的道路吗?

最佳答案

用法:

        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 容器。

关于c# - 在 WebClient 中接受 Cookies?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551345/

相关文章:

c# - 如何将字符串数组拆分为新的字符串数组并删除重复项

c# - 如何通过 photon unity network - unity 3d 实例化预制件来生成对象

c# - 使用正则表达式 C# 替换 Unicode(泰米尔语)字符串

html - <div> 长度自动适配内容长度

ASP.Net FormsAuthentication Redirect 丢失 Redirect 和 Application_AuthenticateRequest 之间的 cookie

c# - 有没有一种技术可以将 DebugFormat() 与构建起来很昂贵的参数一起使用?

javascript - 如果我有多个 ul 并且所有 li 都有相同的类名,我怎样才能获得特定于 ul 的 li

HTML 类引用

android - 为 Android WebView 启用第三方 cookie

asp.net - IdentityServer4 中的 Cookie