簡體   English   中英

如何在.NET中為我的應用程序設置代理設置

[英]How to set proxy settings for my application in .NET

我在C#中有一個應用程序,它使用服務引用通過Web服務發送SMS。 用戶的Internet連接需要通過代理服務器才能到達世界。

所以我的問題是如何告訴.NET通過代理調用Web服務? 還是像在YMahoo Messenger中一樣,如何為我的應用程序的Internet連接設置代理設置?

我也想讓用戶選擇代理設置。

我相信您正在尋找的是配置文件中的defaultProxy

這是鏈接中的示例:

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

請嘗試以下代碼,希望對您有所幫助

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM