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

问题描述 投票:0回答:1

我正在尝试从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";,但到目前为止,这些都没有帮助我。

下面是使用的代码:

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();

请帮助。

wcf http-status-code-403
1个回答
0
投票

可能有很多事情。测试您的代码,由于git的要求,我可能会注意到您缺少UserAgent,并且您的Accept是错误的。

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

httprequest.UserAgent = "My request";

如果仍然有问题,您可以自己尝试添加try和catch块,然后检查git

中的reponseText:
catch (WebException exception)
            {
                string responseText;

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

希望有帮助

© www.soinside.com 2019 - 2024. All rights reserved.