程序是.net開發的winform工具,分服務器端和客戶端,用wcf技術實現數據交互。
客戶端是大型公司,內部統一使用代理服務器上網。具體描述為:在IE中設置lan代理服務器才能查詢網絡數據;登錄QQ或其他聯網程序(網絡版金山詞霸)時,需要打開程序的代理設置,填寫相應的地址和端口,才能登錄。
自行開發的.net程序這類問題怎么解決。
參考這里:http://bbs.csdn.net/topics/390395103
HomeLinkTransClient client = new HomeLinkTransClient(binding, epAddress); client.ClientCredentials.UserName.UserName = "用戶名"; client.ClientCredentials.UserName.Password = "密碼";
此客戶端代理是別人訪問設置的用戶名和密碼
web.config配置才是網絡代理
<system.net>
<defaultProxy useDefaultCredentials="true"></defaultProxy>
</system.net>
以上web.config設置會默認的從瀏覽器中找到代理。
實際應用只在服務器端的web.config里面設置了下面的代碼即可解決問題。
至於上面的client的設置,暫不清楚用處。
補充一:上面的方法並未解決實際用戶的問題。
wcf的客戶端代理為System.ServiceModel.ClientBase<IWcfService>類型的對象,每次鏈接wcf都是實例化此對象,然后發送請求。
本次的解決方法:
public class WcfClient { static readonly System.ServiceModel.BasicHttpBinding _Binding; /// <summary> /// 默認wcf服務連接 /// </summary> public const string UriString = @"http://www.123.cn/wcf.svc"; static WcfClient() { _Binding = new BasicHttpBinding(); _Binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() { MaxStringContentLength = 65536 }; //(更改這個數字) //在這里設置代理 WebProxy proxy = new WebProxy("192.168.0.200:8765", false); proxy.Credentials = new NetworkCredential("User", "psd"); System.Net.HttpWebRequest.DefaultWebProxy = proxy; } /// <summary> /// 獲取wcf服務 /// </summary> /// <param name="uri">wcf服務連接</param> /// <returns></returns> public static WcfServiceClient GetService(string uri = UriString) { var sc = new WcfServiceClient(); sc.Endpoint.Address = new EndpointAddress(new Uri(uri));
sc.Endpoint.Binding = _Binding; return sc; } }
在上面的靜態構造函數里面配置代理的屬性,賦值給System.Net的默認代理設置即可。
遺留問題:通常代理的設置都有http/sockS5/sockS4/瀏覽器設置等,這里的代理設置應該是對http的代理設置,這幾種代理設置是針對代理服務器的還是wcf數據傳輸方式?感覺應該是代理服務器的,如果是這樣,那其他幾種代理方式的程序設置應該怎么完成呢?
沒看懂的資料:http://bbs.csdn.net/topics/110181980
補充二:
http://code.logos.com/blog/2010/01/using_http_proxy_servers.html