前段时间,在公司做了个Remoting 的程序,主要是验证http通道与tcp通道的通信方法与性能.
刚开始,我做了个Server,利用Iss做宿主,(Server,Client都在公司网域内)
可发现在局域网(访问外网需要通过代理服务器),利用http通道提示我需要 通过代理验证,才能访问,几经周折,在网上查了好几天,终于找出利用http通道可以通过代理访问,以下是我在客户端的注册代理程序代码
System.Net.WebProxy wp = new System.Net.WebProxy(代理服务器:端口)
wp.BypassProxyOnLocal = false;
wp.Credentials = new NetworkCredential(username, pwd, 域);
System.Net.GlobalProxySelection.Select = wp;
//TcpChannel channel = new TcpChannel();
//ChannelServices.RegisterChannel(channel);
//channel = (TcpChannel)ChannelServices.GetChannel("tcp");
//SetChannelProxy(channel, wp);
HttpChannel theChannel = new HttpChannel();
ChannelServices.RegisterChannel(theChannel);
theChannel = (HttpChannel)ChannelServices.GetChannel("http");
setChannelProxy(theChannel, wp);
private static void setChannelProxy(HttpChannel channel, IWebProxy proxy)
{
//if configuration not set proxyserver then set channel's proxyName Properties to null
if (proxy == null)
{
//channel.Properties["proxyName"] = null;
//saveProxyToReg(null,null,null,null,null);
return;
}
//channel.Properties["proxyName"]=null;
//use reflection to set the channel's proxyObject
FieldInfo clientChannelFieldInfo =
typeof(HttpChannel).GetField("_clientChannel",
BindingFlags.Instance | BindingFlags.NonPublic);
HttpClientChannel clientChannel = (HttpClientChannel)
clientChannelFieldInfo.GetValue(channel);
FieldInfo x =typeof(HttpClientChannel).GetField ("_proxyObject",BindingFlags.Instance|BindingFlags.NonPublic );
x.SetValue(clientChannel, proxy);
// use GlobalProxySelection to set other web request
GlobalProxySelection.Select = proxy;
}
可问题是,我在客户端利用http通道访问外网时,每次返回竟然需要2-3秒钟,这种速度是我不能接受的,(不知是程序原因还是别哪个地方需要改进,可我的程序非常简单,只需要返回一个字符,也需要这么长时间,是不是利用iis做宿主,速度会慢一些,因为iis 的安全机制等原因???,),听说利用tcp通道,速度很快,而且不需要通过代理就可以访问外网,
于是我在客户端利用tcp 通道访问外网,可提示我目标机器连不上等错误,我刚开始以为tcp也需要代理验证,可一查证,tcp通道根本没有注册代理的方法,几经辗转,后来发现是局域网的防护墙搞的鬼,只需在防护墙中新增一个协议,开设一个端口出站访问外网即可.
本文介绍了作者在局域网环境下使用HTTP与TCP通道进行通信的实际经验。针对HTTP通道需要代理验证的问题,给出了具体的代码实现,并分析了访问外网时的速度瓶颈。同时探讨了TCP通道在穿越局域网防火墙时遇到的挑战及解决方案。
139





