登陸

用戶

上一篇:WPF實現圖中效果占用CPU資源過高,求解決方案 下一篇:我用vs2013開發的C#寫的form應用程序連接mysql總是提示未能找到命名空間,已經引用了mysql.data

轩辕传奇2018战力总榜:c#搭建一個代理服務器,需要給瀏覽器返回什么數據

榮華軟件  1485  0   2017-12-22 21:03

轩辕传奇职业推荐2019 www.sjxvw.icu 想寫一個代理服務器,(就是客戶在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("//"))
                {
                    URL = URL.Replace("//","");
                }
                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);
            }
            
        }

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

0條回答

登陸后可回復

  1. 登陸
  2. 注冊
服務熱線

400-692-8081

( 周一至周五 09:00-17:00 )

聯系我們

名稱:煙臺榮華軟件科技有限公司

地址:煙臺市高新區航天路101號C棟5樓516

座機:0535-3458081

郵箱:[email protected]

Q Q:327195471

[email protected] 煙臺榮華軟件科技有限公司 版權所有 魯ICP備14003838號-1