日期:2014-05-18 浏览次数:20779 次
/*proxy.cs*/ using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; using GetHttp; namespace WebProxy { class Proxy { public Proxy(Socket socket) { this.clientSocket = socket; } Socket clientSocket; Byte[] read = new byte[1024]; //定义一个空间,存储来自客户端请求数据包 Byte[] Buffer = null; Encoding ASCII = Encoding.ASCII; //设定编码 Byte[] RecvBytes = new Byte[1024]; //定义一个空间,存储Web服务器返回的数据 //Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); public void Run() { string clientmessage = ""; //存放来自客户端的HTTP请求字符串 string URL = ""; //存放解析出地址请求信息 //int bytes = ReadMessage(read, ref clientSocket, ref clientmessage); ReadMessage(read, ref clientSocket, ref clientmessage); Byte[] ReadBrower = Encoding.ASCII.GetBytes(clientmessage); if (clientmessage.Length == 0||clientmessage == null) { 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); //---------------------------------------------- //GetHttp.GetHttp http = new GetHttp.GetHttp(); //string Host = http.GetHost(clientmessage); //string Method = http.GetMethod(clientmessage); //string Url = http.URL(clientmessage); //string Version = (http.HttpVersion(clientmessage)) ? "HTTP/1.1" : "HTTP/1.0"; //string Connection = http.GetConnection(clientmessage); //string Accept = http.GetAccept(clientmessage); //string Accept_Language = http.GetAccept_Language(clientmessage); //string Accept_Encoding = http.GetAccept_Encoding(clientmessage); //string User_Agent = http.GetUser_Agent(clientmessage); //string Send_Message_Web = Method + " " + Url + " " + Version + "\r\n" // + "Host: " + Host + "\r\n" // + "Accept: " + Accept + "\r\n" // + "Accept-Language: " + Accept_Language + "\r\n" // + "User-Agent: " + User_Agent + "\r\n" // + "Accept-Encoding: " + Accept_Encoding + "\r\n" // + "Connection: " + Connection + "\r\n" // + "\r\n"; //---------------------------------------- try { Console.WriteLine(URL); IPHostEntry IPHost = Dns.GetHostEntry(URL); Console.WriteLine("远程主机名: " + IPHost.HostName); string[] aliases = IPHost.Aliases; IPAddress[] address = IPHost.AddressList; Console.WriteLine("Web服务器IP地址:" + address[0]); //解析出要访问的服务器地址