登陆

用户

上一篇:WPF实现图中效果占用CPU资源过高,求解决方案 下一篇:我用vs2013开发的C#写的form应用程序连接mysql总是提示未能找到命名空间,已经引用了mysql.data

c#搭建一个代理服务器,需要给浏览器返回什么数据

荣华软件  341  0   2017-12-22 21:03

想写一个代理服务器,(就是客户在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);
            }
            
        }

下断点看,消息是发了,客户机也接受到了,不过浏览器依然无法显示?是不是应该发特定格式的数据包?还是往什么端口发?

0条回答

登陆后可回复

  1. 登陆
  2. 注册
服务热线

400-692-8081

售前咨询(09:00-18:00)

联系我们

名称:烟台荣华软件科技有限公司

地址:烟台市高新区航天路101号C栋5楼516

座机:0535-3458081

邮箱:001@rongsoft.com

Q Q:327195471

关注我们
  • 微信公众号

  • 微信小程序

  • Android 端

  • iphone 端

Copyright@2018 烟台荣华软件科技有限公司 版权所有 鲁ICP备14003838号-1