获取远程服务器返回错误403禁止异常

Getting the remote server returned an error 403 forbidden exception

我正在尝试从 WCF 服务访问 https://api.github.com/search/repositories?q=service+language:assembly%26sort=stars%26order=desc 并遇到 the remote server returned an error (403) forbidden 异常。

我已经尝试添加 httprequest.UseDefaultCredentials = true;httprequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; 但 none 到目前为止对我有帮助。

下面是使用的代码:

HttpWebRequest httprequest = (HttpWebRequest)HttpWebRequest.Create("https://api.github.com/search/repositories?q=service+language:assembly%26sort=stars%26order=desc");
httprequest.Proxy = new WebProxy()
{
   Address = new Uri(Proxy_address),
   Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
};
httprequest.UseDefaultCredentials = true;
httprequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
httprequest.KeepAlive = true;
HttpWebResponse GISTResponse = (HttpWebResponse)httprequest.GetResponse();

请帮忙。

它可以是很多东西。 测试你的代码,我注意到你缺少 UserAgent 并且你的 Accept 是错误的,由于 [=28 的要求=].

尝试删除接受并添加此行:

httprequest.UserAgent = "My request";

如果您仍然有问题,您可以自己检查发生了什么,添加一个 try 和这个 catch 块,并检查来自 git:[=12 的 reponseText =]

catch (WebException exception)
            {
                string responseText;

                using (var reader = new StreamReader(exception.Response.GetResponseStream()))
                {
                    responseText = reader.ReadToEnd();
                }
            }

希望对您有所帮助