Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp is not honoring RestRequest request.Credentials - can't get through http proxy

With the simple code below, RestClient is not able to get through the web proxy. It is not honoring the value of request.Credentials

I am able to get to the site I'm trying to query in the browser; but the RestClient is blocked by my company's proxy.

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;
}

So what I end up with in textBox1 is html that renders to:

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

In other words, RestSharp is not transmitting the default credentials to the web proxy like its supposed to.

like image 231
developerwjk Avatar asked Feb 05 '23 12:02

developerwjk


1 Answers

I found the answer:

        client.Proxy = new WebProxy(myProxyUrl);
        client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

So it can't simply read the proxy url from your Windows setup as for example HttpWebRequest does. You have to supply the proxy url.

like image 121
developerwjk Avatar answered Feb 07 '23 03:02

developerwjk