日期:2014-05-18  浏览次数:20779 次

求助!一个简单的http协议服务
这几天在弄一个简单的http代理:就是获取本地浏览器发出的http请求从中提取Host然后在重新建个socket与远程web服务器通信并把web服务器的响应数据通过这个程序发送给浏览器,让浏览器显示出网页。代码如下

C# code

/*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]);
                //解析出要访问的服务器地址