public static string PostData(string file)
        {
            string strHTML = "";//用来保存获得的HTML代码

            string sendString;
            sendString = "GET //../../../../../../../.." + file + " HTTP/1.1\r\n";
            sendString += "Cookie: SESSIONID=1531169f08777da4\r\n\r\n";
            sendString += "Host: aaa.com\r\n";
            sendString += "Connection: Keep-alive\r\n";
            sendString += "Accept-Encoding: gzip,deflate\r\n";
            sendString += "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.63 Safari/537.36\r\n";
            sendString += "Accept: */*\r\n";

            //sendString = string.Format(sendString, file);
            Byte[] ByteGet = Encoding.GetEncoding("utf-8").GetBytes(sendString);
            IPAddress hostadd = Dns.GetHostEntry("aaa.com").AddressList[0];
            IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(EPhost);
            if (!s.Connected)
            {
                strHTML = "链接主机失败";
            }
            s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
            strHTML = Recv(s, Encoding.GetEncoding("utf-8"));
            return strHTML;
        }

        public static String Recv(Socket sock, Encoding encode)
        {
            Byte[] buffer = new Byte[1024000000];
            StringBuilder sb = new StringBuilder();

            Thread.Sleep(2500);//根据页面响应时间进行微调        
            Int32 len = sock.Receive(buffer);
            Console.WriteLine("socket.Available:" + sock.Available);
            sb.Append(encode.GetString(buffer, 0, len));

            while (sock.Available > 0)
            {
                Thread.Sleep(1000);//也可以视情况微调           
                Array.Clear(buffer, 0, buffer.Length);
                len = sock.Receive(buffer);
                Console.WriteLine("socket.Available:" + sock.Available);
                Console.WriteLine("接收:" + len + " B");
                sb.Append(encode.GetString(buffer, 0, len));
                string ss = encode.GetString(buffer, 0, len);
            }
            sock.Close();
            return sb.ToString();
        }

 

posted on 2021-05-01 19:09  你不知道的浪漫  阅读(60)  评论(0编辑  收藏  举报