我正在尝试从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();
请帮助。
可能有很多事情。测试您的代码,由于git的要求,我可能会注意到您缺少UserAgent,并且您的Accept是错误的。
尝试删除接受并添加此行:
中的reponseText:httprequest.UserAgent = "My request";
如果仍然有问题,您可以自己尝试添加try和catch块,然后检查git
catch (WebException exception) { string responseText; using (var reader = new StreamReader(exception.Response.GetResponseStream())) { responseText = reader.ReadToEnd(); } }
希望有帮助