Fariborz Navidan
Fariborz Navidan

Reputation: 91

How to set proxy settings for my application in .NET

I have an application in C# which uses a service reference to send SMS through a web service. The user's internet connection needs to pass through a proxy server in order to reach the world.

So my question is how to tell .NET to call web service through the proxy? Or how to set proxy settings for internet connection of my application just like what you can do in YMahoo Messenger?

Also I want to let user to choose proxy settings.

Upvotes: 1

Views: 27524

Answers (2)

scottheckel
scottheckel

Reputation: 9244

I believe what you are looking for is defaultProxy in your config file.

Here's an example from the link:

<configuration>
  <system.net>
    <defaultProxy>
      <proxy
        usesystemdefault="true"
        proxyaddress="http://192.168.1.10:3128"
        bypassonlocal="true"
      />
      <bypasslist>
        <add address="[a-z]+\.contoso\.com" />
      </bypasslist>
    </defaultProxy>
  </system.net>
</configuration>

Upvotes: 6

Prabhakantha
Prabhakantha

Reputation: 660

Please try below code hope this will help you

tring targetUrl = "http://www.google.com";
string proxyUrlFormat = "http://zend2.com/bro.php?u={0}&b=12&f=norefer";
string actualUrl = string.Format(proxyUrlFormat, HttpUtility.UrlEncode(targetUrl));

// Do something with the proxy-ed url
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(actualUrl));
HttpWebResponse resp = req.GetResponse();

string content = null;
using(StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
    content = sr.ReadToEnd();
}

Console.WriteLine(content);

Upvotes: 3

Related Questions