【问题标题】:HTTPWebRequest 403 Forbidden - faking web browserHTTPWebRequest 403 Forbidden - 伪造网络浏览器
【发布时间】:2016-09-24 04:55:45
【问题描述】:

这是我伪造浏览器请求的代码:

HttpWebRequest webReq = WebRequest.CreateHttp(url);
webReq.CookieContainer = new CookieContainer();
webReq.Method = "GET";
webReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0";
webReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webReq.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
webReq.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");

webReq.AllowAutoRedirect = true;
webReq.MaximumAutomaticRedirections = 50;

using (WebResponse response = webReq.GetResponse())
{
    using (Stream stream = response.GetResponseStream())
    {
        StreamReader reader = new StreamReader(stream);
        return reader.ReadToEnd();
    }
}

但我收到此 URL 的 403 HTTP 错误:http://www.alchourouk.com/xml_top_article/rss.xml

我不明白为什么,没有身份验证,没有使用 cookie...

在 Postman 中,我没有错误,这是它生成的代码:

var client = new RestClient("http://www.alchourouk.com/xml_top_article/rss.xml");
var request = new RestRequest(Method.GET);
request.AddHeader("postman-token", "1b40ceba-6311-0fe2-1566-62a2d59950a0");
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);

这里没什么特别的。 知道为什么我的代码会出现 403 错误吗?

【问题讨论】:

    标签: c# httpwebrequest http-status-code-403


    【解决方案1】:

    您需要的是Referer HTTP 标头:

    webReq.Referer = "http://www.alchourouk.com/";
    

    它会工作得很好。

    Live demo here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多