RestSharp 不支持 RestRequest request.Credentials - 无法通过 http 代理
RestSharp is not honoring RestRequest request.Credentials - can't get through http proxy
使用下面的简单代码,RestClient 无法通过网络代理。它不尊重 request.Credentials
的价值
我可以访问我想在浏览器中查询的站点;但是 RestClient 被我公司的代理屏蔽了。
try
{
RestClient client = new RestClient("http://services.groupkt.com/country/get/all");
RestRequest request = new RestRequest(Method.POST);
//set credentials to default
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
//also tried using client.UserAgent to spoof Firefox user-agent to no avail
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
IRestResponse response = client.Execute(request);
textBox1.Text = response.Content;
}
catch (Exception ex)
{
textBox1.Text = "ERROR:" + ex.Message;
}
所以我最终在 textBox1 中得到的是 html 呈现为:
Authorized Only
Secure Web Gateway has blocked your request because you have not been
authorized and authorization is required.
URL:
User Name / Source: / 10.xx.xx.xx
Rule Set: Authentication with Kerberos and NTLM Fallback / Authenticate
With Kerberos (don't evaluate NTLM tokens)
IMPORTANT: when you access internet web pages you should comply with authorizations approvals according to [CompanyName] Internet Filtering.
generated 2017-05-05 15:04:08
RestSharp 104.1.0.0
换句话说,RestSharp 没有像预期的那样将默认凭据传输到 Web 代理。
我找到了答案:
client.Proxy = new WebProxy(myProxyUrl);
client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
因此它不能像 HttpWebRequest 那样简单地从您的 Windows 设置中读取代理 url。您必须提供代理 url.
使用下面的简单代码,RestClient 无法通过网络代理。它不尊重 request.Credentials
的价值我可以访问我想在浏览器中查询的站点;但是 RestClient 被我公司的代理屏蔽了。
try
{
RestClient client = new RestClient("http://services.groupkt.com/country/get/all");
RestRequest request = new RestRequest(Method.POST);
//set credentials to default
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
//also tried using client.UserAgent to spoof Firefox user-agent to no avail
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
IRestResponse response = client.Execute(request);
textBox1.Text = response.Content;
}
catch (Exception ex)
{
textBox1.Text = "ERROR:" + ex.Message;
}
所以我最终在 textBox1 中得到的是 html 呈现为:
Authorized Only
Secure Web Gateway has blocked your request because you have not been authorized and authorization is required.
URL:
User Name / Source: / 10.xx.xx.xx
Rule Set: Authentication with Kerberos and NTLM Fallback / Authenticate
With Kerberos (don't evaluate NTLM tokens)
IMPORTANT: when you access internet web pages you should comply with authorizations approvals according to [CompanyName] Internet Filtering.
generated 2017-05-05 15:04:08
RestSharp 104.1.0.0
换句话说,RestSharp 没有像预期的那样将默认凭据传输到 Web 代理。
我找到了答案:
client.Proxy = new WebProxy(myProxyUrl);
client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
因此它不能像 HttpWebRequest 那样简单地从您的 Windows 设置中读取代理 url。您必须提供代理 url.