1

如何使用 HttpWebRequest 下载 pdf 文件?我想下载 pdf 文件并将其保存到我的系统我无法使用 WebClient 下载,因为我需要在我的请求中添加 cookie

<code>
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(Url);
    CookieContainer cookieJar = new CookieContainer();
    cookieJar.Add(new Cookie("cookieName", "value", "/", "domain));      
    (Request as HttpWebRequest).CookieContainer = cookieJar;
     HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();


</code>
4

2 回答 2

0

使用 HttpWebRequest GetResponseStream()将响应的主体作为流获取,然后将其保存到文件中。

于 2012-11-07T18:00:37.087 回答
0

尝试这样的事情: -

string GetPage(string path) {
 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(path);
req.CookieContainer = cookie;
WebResponse resp = req.GetResponse();
string t = new StreamReader(resp.GetResponseStream(), Encoding.Default).ReadToEnd();
return IsoToWin1250(t);
}
于 2012-11-07T18:02:20.827 回答