将cookie添加到WebClient

我想用WebClient下载pdf,但我需要发送cookie请求

如何将cookie添加到WebClient然后下载pdf

  WebClient webClient = new WebClient(); webClient.DownloadFile("http://........", "C://2.pdf"); 

您可以向webClient添加cookie。

 webClient.Headers.Add(HttpRequestHeader.Cookie, "cookies"); webClient.DownloadFile("http://........", "C://2.pdf"); 

WebClient不支持cookie。 您需要自己重新添加对GetWebRequest支持,或者只使用HttpWebRequest类。

示例代码在这里。