接受WebClient中的Cookie?

时间:2013-01-27 19:34:33

标签: c# html cookies webclient

我刚开始尝试使用C#WebClient。我所拥有的是下面的代码,它从网站获取html代码并将其写入.txt文件。我唯一的问题是有些网站要求您在使用网站之前接受cookie。这导致的不是将真实的网站html代码写入.txt文件,而是编写cookie popup 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);
}

那么解决方案是什么?有人可以指引我走正确的道路吗?

3 个答案:

答案 0 :(得分:26)

用法:

        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.Add(cookie);
        }

        return response;
    }
}

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

答案 1 :(得分:4)

只需将标题中的cookie字符串存储到本地会话_cookies字符串

if (System.Web.HttpContext.Current.Session["cookie"] != null)
            _cookies = System.Web.HttpContext.Current.Session["cookie"].ToString(); 

     using (WebClient wc =  new WebClient())
        {

            wc.Headers.Add("Cookie", _cookies);
             string HtmlResult = wc.UploadString(bridge_url, myParameters);
            _cookies = wc.ResponseHeaders["Set-Cookie"];
            Debug.WriteLine("Headers" + _cookies); 

            System.Web.HttpContext.Current.Session["cookie"] = _cookies;

        } 

答案 2 :(得分:2)

这可能与How can I get the WebClient to use Cookies?

密切相关

我上面提到的问题是针对VB.NET的,但C#的机制应该是相同的。我怀疑你看到的行为是网站正在发送一个cookie然后再请求它,但你的客户端没有设置将cookie返回给服务器,所以它解释为你不接受cookie。'

您是否使用像Fiddler这样的分析工具来分析与客户沟通的内容?

您可能还必须发送特定的HTTP标头以表明您接受Cookie,但我不记得在我过去的经历中需要它。