2

我想从网站下载一些图片,但它必须发送带有“referer”的请求。像这样的代码:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
 CookieContainer cc = new CookieContainer();
 request.CookieContainer = cc;
 request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";
 request.Referer = "http://www.xxxxx.com/xxx/xxx/list_78542.html";
 request.CookieContainer = cc;
 HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
 using (Stream stream = myResponse.GetResponseStream())
 ...

但是,当我想在 Windows 商店应用程序中执行此操作时,它在 WebRequest 中没有属性“Referer”,我尝试使用它(如下所示)来设置“UserAgent”和“Referer”

    request.Headers["UserAgent"] = UserAgent;
    request.Headers["Referer"] = Referer;

但它会引发错误“必须使用适当的属性或方法修改'Referer'标头。”

我该怎么办,请帮帮我?

谢谢。

4

2 回答 2

0

尝试使用Windows.Web.Http命名空间而不是System.Net,如此处所述https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn440594.aspx

于 2015-06-16T17:33:20.610 回答
-1

很简单:

myHttpWebRequest.Referer="http://www.microsoft.com";

于 2013-05-12T06:43:27.250 回答