Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a proxy server is configured?

Tags:

c#

proxy

c#-4.0

I have some code that runs fine when I have a web proxy defined in Internet Explorer. However if there is none defined it doesn't work. I want to check if the a proxy is defined. How would I change the below code to do that?

public DataTable GetCurrentFxPrices(string url)
{
    WebProxy wp = new WebProxy(WebProxy.GetDefaultProxy().Address.AbsoluteUri, true);
    wp.Credentials = CredentialCache.DefaultCredentials;

    WebClient wc = new WebClient();
    wc.Proxy = wp;

    MemoryStream ms = new MemoryStream(wc.DownloadData(url));
    DataSet ds = new DataSet("fxPrices");
    ds.ReadXml(ms);

    DataTable dt = ds.Tables["Rate"];
    int i = dt.Rows.Count;
    return dt;
}

For example how would I download the data without using a proxy?

UPDATE

I have changed the code to the following

public DataTable GetCurrentFxPrices(string url)
{
    WebClient wc = new WebClient();

    if (!String.IsNullOrEmpty(WebProxy.GetDefaultProxy().Address.AbsoluteUri))
    {
        WebProxy wp = new WebProxy(WebProxy.GetDefaultProxy().Address.AbsoluteUri, true);
        wp.Credentials = CredentialCache.DefaultCredentials;
        wc.Proxy = wp;
    }            

    MemoryStream ms = new MemoryStream(wc.DownloadData(url));
    DataSet ds = new DataSet("fxPrices");
    ds.ReadXml(ms);
    DataTable dt = ds.Tables["Rate"];

    int i = dt.Rows.Count;
    return dt;
}

I get the following error System.NullReferenceException was unhandled by user code on the if statement line.

UPDATE 2

I've also tried changing this line:

if (!String.IsNullOrEmpty(WebProxy.GetDefaultProxy().Address.AbsoluteUri))

to

if (WebProxy.GetDefaultProxy().Address.AbsoluteUri != null)

but I get this error:

System.NullReferenceException: Object reference not set to an instance of an object.

Any ideas?

like image 510
Mark Allison Avatar asked Feb 04 '11 13:02

Mark Allison


People also ask

How do you find the proxy server?

To set up a proxy server for a VPN connectionSelect the Start button, then select Settings > Network & Internet > VPN. Select the VPN connection, then select Advanced options. Under VPN proxy settings, select the type of proxy setup you want to use, then enter the proxy server information for that VPN connection.

How can I see proxy settings in CMD?

Select the “Start” button, then type “cmd“. Right-click “Command Prompt“, then choose “Run as Administrator“. Type “netsh winhttp show proxy“, then press “Enter“.


1 Answers

Remember, there is not one single "proxy address" or proxy Uri as you might think. Instead the proxy Uri may depend on each Uri to be retrieved, as can be seen in Internet Explorer's Proxy Settings dialog.

Internet Explorer - Proxy Settings dialog

The IWebProxy interface helps you to get the right proxy Uri, and tells you if this proxy will be used or bypassed for a specific Uri to be retrieved.

using System.Net;

Uri exampleUri = new Uri("http://www.example.org/")

IWebProxy defaultProxy = WebRequest.GetSystemWebProxy();

bool isBypassed = defaultProxy.IsBypassed(exampleUri);
// ... false

Uri proxyUri = defaultProxy.GetProxy(exampleUri);
// ... http://someproxy.mycorp.example:8080

In your method you would have to pass an IWebProxy interface, not a proxy address. The default proxy interface (like from GetSystemWebProxy) is always set as a default.

If you want to set your own special proxy in case there is no proxy used for your Uri, you could do the following ...

public DataTable GetCurrentFxPrices(string url)
{
    Uri uri = new Uri(url);

    WebClient webClient = new WebClient();
    IWebProxy defaultProxy = WebRequest.GetSystemWebProxy();

    IWebProxy myProxy = new WebProxy(new Uri("http://myproxy:8080"))
    // if no bypass-list is specified, all Uris are to be retrieved via proxy

    if (defaultProxy.IsBypassed(uri))
    {
        myProxy.Credentials = CredentialCache.DefaultCredentials;
        webClient.Proxy = myProxy;
    }            

    MemoryStream ms = new MemoryStream(webClient.DownloadData(url));
    DataSet ds = new DataSet("fxPrices");
    ds.ReadXml(ms);
    DataTable dt = ds.Tables["Rate"];

    int i = dt.Rows.Count;
    return dt;
}
like image 143
oleschri Avatar answered Oct 26 '22 22:10

oleschri