如何理解.NET Remoting中的Proxy设定
在.NET中,与Proxy相关的类有以下几个,
通用的类:
System.Net.WebProxy,
System.Net.GlobalProxySelection,
可以用下面的方法设定,通用的Proxy
string proxyserver = “192.168.2.1”;
int proxyport = 8080;
string ProxyUserName = “zs03”;
string ProxyUserName = “mypass”;
string ProxyDomain = “myDomain”;
WebProxy proxy = new WebProxy(proxyserver, proxyport);
NetworkCredential credentials = new NetworkCredential(ProxyUserName,
ProxyPassword,ProxyDomain);
proxy.Credentials = credentials;
GlobalProxySelection.Select = proxy;
同时,ChannelHttpClient中,也可以设定Proxy, 比如可以用下面一段代码来设定ChannelHttpclient的Proxy,
string proxyserver = “192.168.2.1”;
int proxyport = 8080;
string ProxyUserName = “zs03”;
string ProxyUserName = “mypass”;
string ProxyDomain = “myDomain”;
NetworkCredential credentials = new NetworkCredential(ProxyUserName, ProxyPassword, ProxyDomain);
Hashtable hash = new Hashtable();
hash.Add("credentials",credentials);
hash.Add("proxyName",proxyserver);
hash.Add("proxyPort",proxyport);
ChannelHttpClient = new HttpClientChannel(hash,new BinaryClientFormatterSinkProvider());
ChannelServices.RegisterChannel(ChannelHttpClient);
一般来说,GlobalProxySelection用来确定在默认的情况下,Proxy的设定的情况,如果不进行设定的话,一般系统会自动取确省的设置
那怎样才能把确省的设制去掉呢?
对于通用的设置,可以用下面的方法:
GlobalProxySelection.Select = GlobalProxySelection.GetEmptyWebProxy();
对于ChannelHttpClient的话可以用:
ChannelHttpClient = new HttpClientChannel("http",new BinaryClientFormatterSinkProvider());
ChannelHttpClient.Properties["ProxyName"] = null;
ChannelServices.RegisterChannel(ChannelHttpClient);
如果采用配置文件的话, 一般只用配置WebProxy就可以了, 如果要自己定义Remoting Channel的话则要定义ChannelHttpClient的Proxy
注: 一般的错误信息如下:
The remote server returned an error: (407) Proxy Authentication Required.