技術社區

c#搭建一個代理服務器,需要給瀏覽器返回什么數據
admin | 發表于: 2017-12-22 回答
想寫一個代理服務器,(就是客戶在IE設置里填入代理服務器,端口8000)

現在的情況,可以收集客戶的數據包,也轉發了HTTP請求,通過斷點也檢測到了,返回了客戶請求的網頁源碼。

最后返回給客戶端的時候,客戶瀏覽器死活顯示不出來,應該返回什么樣的數據?不能直接把web的數據轉發過去么?

上代碼:

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))
            {
                return;
                throw new IOException();
            }
            URL = clientmessage.Substring(index1 + 1, index2 - index1 -1);
            try
            {
                int ports = 80;
                if (URL.Contains("http://"))
                {
                    URL = URL.Replace("http://","");
                }
                else if (URL.Contains("https://"))
                {
                    URL = URL.Replace("https://", "");
                    ports = 443;
                }
                if (URL.Contains(':'))
                {
                    int index3 = URL.IndexOf(':');
                    ports = int.Parse(URL.Substring(index3 + 1));
                    URL = URL.Substring(0, index3);
                }
                IPHostEntry IPHost = Dns.GetHostEntry(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], ports);
                
                Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //創建連接Web服務器端的Socket對象
                IPsocket.Connect(address[0], ports);
                //Socket連Web接服務器
                if (IPsocket.Connected)
                {
                    //Socket 正確連接!
                }
                    
                string GET = clientmessage;
                Byte[] ByteGet = ASCII.GetBytes(GET);
                IPsocket.Send(ByteGet, ByteGet.Length, 0);
                //代理訪問軟件對服務器端傳送HTTP請求命令
                Int32 rBytes = 0;
                try
                {
                    rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
                    SendMessage(ref clientSocket, ASCII.GetString(RecvBytes, 0, rBytes));
                }
                catch(SocketException ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                //代理訪問軟件接收來自Web服務器端的反饋信息
                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);
                    SendMessage(ref clientSocket, ASCII.GetString(RecvBytes, 0, rBytes));
                }
                IPsocket.Shutdown(SocketShutdown.Both);
                IPsocket.Close();


//sendmessage 函數
void SendMessage(ref Socket s, string message)
        {
            if (message != null)
            {
                Buffer = new Byte[message.Length + 1];
                int length = ASCII.GetBytes(message, 0, message.Length, Buffer, 0);
                s.Send(Buffer, length, 0);
            }
            
        }

下斷點看,消息是發了,客戶機也接受到了,不過瀏覽器依然無法顯示?是不是應該發特定格式的數據包?還是往什么端口發?

回復列表

請先登錄,登錄后回復!

登錄 注冊
3d专家预测组三组六方法