【ChinaBeta.Cn 网盟学院】
(2). 另外一个Socket是在代理服务程序侦听端口号,接收挂起的连接请求时候得到的,以此Socket为参数,利用Proxy类中的构造函数,来创建一个Proxy实例的。此Socket实现从客户端接收HTTP请求信息,并传送数据到客户端。
Socket创建和使用是实现Web代理软件的关键,具体实现方法是在构造函数代码后面,输入下列代码,创建Proxy类的Run方法:
public void Run ( ) { string clientmessage = " " ; //存放来自客户端的HTTP请求字符串 string URL = " " ; //存放解析出地址请求信息 int bytes = ReadMessage ( read , ref clientSocket , ref clientmessage ) ; if ( bytes == 0 ) { return ; }
int index1 = clientmessage.IndexOf ( ' ' ) ; int index2 = clientmessage.IndexOf ( ' ' , index1 + 1 ) ; if ( ( index1 == -1 ) || ( index2 == -1 ) ) { throw new IOException ( ) ; } string part1 = clientmessage.Substring ( index1 + 1 , index2 - index1 ) ; int index3 = part1.IndexOf ( '/' , index1 + 8 ) ; int index4 = part1.IndexOf ( ' ' , index1 + 8 ) ; int index5 = index4 - index3 ; URL = part1.Substring ( index1 + 4 , ( part1.Length - index5 ) - 8 ) ;
try { IPHostEntry IPHost = Dns.Resolve ( URL ) ; Console.WriteLine ( "远程主机名: " + IPHost.HostName ) ; string [] aliases = IPHost.Aliases ; IPAddress[] address = IPHost.AddressList ; Console.WriteLine ( "Web服务器IP地址:" + address[0] ) ; //解析出要访问的服务器地址 IPEndPoint ipEndpoint = new IPEndPoint ( address[0] , 80 ) ; Socket IPsocket = new Socket ( AddressFamily.InterNetwork , SocketType.Stream , ProtocolType.Tcp ) ; //创建连接Web服务器端的Socket对象 IPsocket.Connect ( ipEndpoint ) ; //Socket连Web接服务器 if ( IPsocket.Connected ) Console.WriteLine ( "Socket 正确连接!" ) ; string GET = clientmessage ; Byte[] ByteGet = ASCII.GetBytes ( GET ) ; IPsocket.Send ( ByteGet , ByteGet.Length , 0 ) ; //代理访问软件对服务器端传送HTTP请求命令 Int32 rBytes = IPsocket.Receive ( RecvBytes , RecvBytes.Length , 0 ) ; //代理访问软件接收来自Web服务器端的反馈信息 Console.WriteLine ( "接收字节数:" + rBytes.ToString ( ) ) ; String strRetPage = null ; strRetPage = strRetPage + ASCII.GetString ( RecvBytes , 0 , rBytes ) ; while ( rBytes > 0 ) { rBytes = IPsocket.Receive ( RecvBytes , RecvBytes.Length , 0 ) ; strRetPage = strRetPage + ASCII.GetString ( RecvBytes , 0 , rBytes ) ; } IPsocket.Shutdown ( SocketShutdown.Both ) ; IPsocket.Close ( ) ; SendMessage ( clientSocket , strRetPage ) ; //代理服务软件往客户端传送接收到的信息 } catch ( Exception exc2 ) { Console.WriteLine ( exc2.ToString ( ) ) ; } } //接收客户端的HTTP请求数据 private int ReadMessage ( byte [ ] ByteArray , ref Socket s , ref String clientmessage ) { int bytes = s.Receive ( ByteArray , 1024 , 0 ) ; string messagefromclient = Encoding.ASCII.GetString ( ByteArray ) ; clientmessage = ( String )messagefromclient ; return bytes ; } //传送从Web服务器反馈的数据到客户端 private void SendMessage ( Socket s , string message ) { Buffer = new Byte[message.Length + 1] ; int length = ASCII.GetBytes ( message , 0 , message.Length , Buffer , 0 ) ; Console.WriteLine ( "传送字节数:" + length.ToString ( ) ) ; s.Send ( Buffer , length , 0 ) ; } | 9. 在定义Proxy类代码区中加入下列代码,下列代码是定义Proxy类中的使用的一些变量,这些变量主要是在后面的定义Run方法中使用。
Socket clientSocket ; Byte[] read = new byte[1024] ; //定义一个空间,存储来自客户端请求数据包 Byte [] Buffer = null ; Encoding ASCII = Encoding.ASCII ; //设定编码 Byte[] RecvBytes = new Byte[4096] ; //定义一个空间,存储Web服务器返回的数据 | 10. 至此,Proxy类的定义过程就完成了。把Proxy类实例化非常简单,和以前用的其他完全一样,具体语法如下:
| public Proxy ( Socket socket ); | 参数:socket为一个Scoket实例
下面代码是创建一个Proxy实例:
| Proxy proxy = new Proxy ( socket ) ; |
(二). 利用Proxy类,实现Web代理的具体示例:
下面是利用上面创建的Proxy类,实现Web代理程序的具体实现步骤,Proxy类被定义在命名空间WebProxy中。
1. 在Visual Studio .Net的代码编辑器中打开Class1.cs文件,进入Class1.cs的代码编辑界面。
2. 在Class1.cs源文件的开头导入下列命名空间:
using System ; using System.Net ; using System.Net.Sockets ; using System.Text ; using System.IO ; using System.Threading ; using WebProxy ; //其中命名空间WebProxy是Proxy类所处的位置,具体可以参阅Proxy.cs源文件 //中命名空间的定义。 | 3. 在Main函数中添加下列代码,下列代码是利用Proxy类,来实现Web代理程序。
const int port = 8000 ; //定义端口号 TcpListener tcplistener = new TcpListener ( port ) ; Console.WriteLine ( "侦听端口号: " + port.ToString ( ) ) ; tcplistener.Start ( ) ; //侦听端口号 while ( true ) { Socket socket = tcplistener.AcceptSocket ( ) ; //并获取传送和接收数据的Scoket实例 Proxy proxy = new Proxy ( socket ) ; //Proxy类实例化 Thread thread = new Thread ( new ThreadStart ( proxy.Run ) ) ; //创建线程 thread.Start ( ) ; //启动线程 } | 保存上面的所有步骤,这样一个简单Web代理程序就算是完成了。此Web代理程序侦听的是8000端口号。
(三).测试Web代码程序:
Web代理程序要通过二台计算机才能够实现。其中的一台计算机运行Web代理程序,充当Web代理服务器。另外一台计算机充当客户机,通过Web代理服务器来浏览网页。在确定Web代理软件运行后,下面是对客户机进行必要的设置。
1. 打开IE浏览器。
2. 选择【工具】|【Internet选项】,弹出【Internet选项】对话框。在此对话框中选择【连接】页面,单击其中的【局域网设置】按钮。弹出【局域网(LAN)设置】对话框。选择【为LAN使用代理服务器(X),(这些设置不会应用于拨号和VPN连接)】多选框。并在其中的【地址】文本框中输入代理服务器的IP地址,由于测试的代理服务器的IP地址为"10.138.198.213",所有也输入此IP地址,在【端口】文本框中输入"8000"。具体如图03所示:
 图03:客户端设定Web代理 服务器对话框 | 此时客户端的设置就完成了,在确定IP地址为"10.138.198.213"的这台计算机已经运行上面介绍的Web代理程序后。打开客户端的IE浏览器,并输入要浏览的网址,就可以通过Web代理服务器来浏览网页了,图04是Web代理服务程序在服务器端运行时的界面。
 图04:Web代理服务程序在 服务器端的运行界面 | 四.总结:
至此一个简单的Web代理服务软件就算基本完成了,通过上面内容的介绍可见,虽然代理服务的实现原理相对简单,但具体实现其实还是很繁琐的。网络代理是一个内容丰富,实现复杂的论题,本节介绍的代理服务软件,无论在实现的协议种类,还是实现的功能,都只能算很小的一部分。希望各位能够通过本文的介绍,结合其他相关的知识,创造出功能更强大、安全性更高,使用更稳定的网络代理服务程序来。 上一页 [1] [2]
(责任编辑:hahack)
|