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

1

5

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.

Cabaret answered 5/5, 2017 at 20:30 Comment(0)
C
9

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.

Cabaret answered 8/5, 2017 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.