18

I'm trying to make a webrequest through a proxy on Windows phone 7. From what I can see the Compact Framework does not include the configuring of a proxy for the HttpWebRequest object. I tried using RestSharp but the RestClient also does not allow this. I've also tried configuring the Internet Options on my local machine to use a proxy, hopping that the same options will apply on my Windows Phone Emulator. That didn't work. Do you have any ideas?

Flaviu Zapca
  • 181
  • 1
  • 1
  • 4

6 Answers6

23

Under "configuration" section in Web.config add this:

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
</system.net>

(for more more info - MSDN - defaultProxy Element (Network Settings))

Zoe
  • 27,060
  • 21
  • 118
  • 148
Israel Margulies
  • 8,656
  • 2
  • 30
  • 26
  • 1
    This just fixed a problem I've been having for 3 days. See my question here: http://stackoverflow.com/questions/26845404/not-able-to-connect-to-website-urls-from-asp-net-webapi-action-methods/27174481#27174481 – xan Nov 27 '14 at 15:49
21

For people coming from Google looking how to set a proxy with RestSharp, if you are not on Windows Phone, at least as of version 104.4.0 you can do the following:

var client = new RestClient("http://example.com")
client.Proxy = new WebProxy("http://proxy.example.com")

Don't know whether this would work on Windows Phone since I am not familiar with the framework there; since the title of the question did not contain Windows Phone I thought that many like myself would end up here, just searching about how to setup the proxy with RestSharp.

Stefano Ricciardi
  • 2,923
  • 3
  • 33
  • 40
  • i had tried the above per Stefano (using webProxy with restSharp's restClient - kept getting proxy connection issues). instead i ended up dumping that and just editing my app.config by adding in this: ` ` received the idea from this post on SO [C# Connecting Through Proxy](http://stackoverflow.com/questions/1938990/c-sharp-connecting-through-proxy). – Justin C Apr 09 '15 at 14:30
  • i understand this doesn't directly answer the win 7 original question, but thought i would share in case another fellow restSharp user was struggling with the webProxy issue. – Justin C Apr 09 '15 at 14:35
  • 1
    Does anyone know how to do this in RestSharp v107? I've used it before with v106 but with v107 it tells me client.Proxy is not valid. I was going to downgrade to v106 but I see it has a know vulnerability. – Caverman Jan 14 '22 at 22:36
  • 1
    @Caverman Checkout my answer below. https://stackoverflow.com/a/71271696/8644294 – Ash K Feb 25 '22 at 21:02
6

In RestSharp v107, you can set Proxy using options object.

var options = new RestClientOptions("https://api.myorg.com") {
    Proxy = GetWebProxy() // <-- Right here.
    ThrowOnAnyError = true,
    Timeout = 1000
};
var client = new RestClient(options);

Method to return WebProxy object:

public static WebProxy GetWebProxy()
{
    var proxyUrl = "http://proxy-name.companydomain.com:9090/";
    // First create a proxy object
    var proxy = new WebProxy()
    {
        Address = new Uri(proxyUrl),
        BypassProxyOnLocal = false,
        //UseDefaultCredentials = true, // This uses: Credentials = CredentialCache.DefaultCredentials
        //*** These creds are given to the proxy server, not the web server ***
        Credentials = CredentialCache.DefaultNetworkCredentials
        //Credentials = new NetworkCredential("proxyUserName", "proxyPassword")
    };

    return proxy;
}

Reference: https://restsharp.dev/v107/#restsharp-v107

Ash K
  • 1,802
  • 17
  • 44
4

This worked for me.

String url = "some url";

IWebProxy proxy = WebRequest.DefaultWebProxy;
proxy.Credentials = CredentialCache.DefaultCredentials;
RestClient client = new RestClient(url);
{
Proxy = proxy,
};
Lucius Kaye
  • 91
  • 3
  • 6
2

Some example using a method and a class: The call:

            var client = new RestClient(urlbase);
            if(myConfigInstance.ProxyActive) {
                 client.Proxy = GetWebProxy(myConfigInstance);
            }

and the method:

  public static WebProxy GetWebProxy(ProxySettings proxySettings)
    {
        WebProxy proxy;
        try
        {
            proxy = new WebProxy(proxySettings.Server, Int32.Parse(proxySettings.Port))
            {
                Credentials = new NetworkCredential(proxySettings.Username, proxySettings.Password, proxySettings.Domain)
            };
        }
        catch (Exception ex)
        {
          throw new Exception("Error");
        }
        return proxy;
    }

and the class:

public class ProxySettings
{
    public bool ProxyActive { get; set; }
    public string Port { get; set; }
    public string Server { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Domain { get; set; }
}
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
0
RestClient client = new RestClient(url);
client.Options.Proxy = new WebProxy("127.0.0.1");

Don't forget .Options.Proxy

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Dec 22 '22 at 15:38