问题
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?
回答1:
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))
回答2:
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.
回答3:
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; }
}
回答4:
This worked for me.
String url = "some url";
IWebProxy proxy = WebRequest.DefaultWebProxy;
proxy.Credentials = CredentialCache.DefaultCredentials;
RestClient client = new RestClient(url);
{
Proxy = proxy,
};
来源:https://stackoverflow.com/questions/12051137/web-request-through-proxy-using-restsharp