如何在.NET中为我的应用程序设置代理设置

时间:2022-05-25 00:20:41

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.

我在C#中有一个应用程序,它使用服务引用通过Web服务发送SMS。用户的互联网连接需要通过代理服务器才能到达世界。

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?

所以我的问题是如何告诉.NET通过代理调用Web服务?或者如何为我的应用程序的互联网连接设置代理设置,就像你在YMahoo Messenger中可以做的那样?

Also I want to let user to choose proxy settings.

另外我想让用户选择代理设置。

2 个解决方案

#1


2  

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);

#2


1  

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

我相信你要找的是配置文件中的defaultProxy。

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>

#1


2  

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);

#2


1  

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

我相信你要找的是配置文件中的defaultProxy。

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>