Ejemplo n.º 1
0
        public static byte[] GetBypes(RequestArgs mReq)
        {
            byte[]         bHtml = null;
            HttpWebRequest req   = (HttpWebRequest)HttpWebRequest.Create(mReq.Url);

            req.Method    = mReq.Method;
            req.Accept    = "*/*";
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            if (mReq.cookie != string.Empty)
            {
                req.Headers.Add(HttpRequestHeader.Cookie, mReq.cookie);
            }
            req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            req.KeepAlive = true;
            if (mReq.RefererUrl != "")
            {
                req.Referer = mReq.RefererUrl;
            }
            req.AllowAutoRedirect = mReq.blRedirect;
            if (mReq.blPolicy == true)
            {
                System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            }
            if (mReq.Method == "POST")
            {
                StreamWriter sw = null;
                sw = new StreamWriter(req.GetRequestStream());
                sw.Write(mReq.postData);
                sw.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream          s    = resp.GetResponseStream();

            byte[] zipBuffer = new byte[1024 * 500];
            int    bytesRead = 0;
            int    offset    = 0;

            while (true)
            {
                bytesRead = s.Read(zipBuffer, offset, zipBuffer.Length - offset);
                if (bytesRead <= 0)
                {
                    break;
                }
                offset += bytesRead;
            }
            if (resp.ContentEncoding == "gzip")
            {
                MemoryStream ms        = new MemoryStream(zipBuffer, 0, offset, true);
                GZipStream   zipStream = new GZipStream(ms, CompressionMode.Decompress, true);
                ms.Seek(0, SeekOrigin.Begin);
                byte[] deBuffer = new byte[1024 * 300];
                offset = 0;
                while (true)
                {
                    bytesRead = zipStream.Read(deBuffer, offset, 1024);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    offset += bytesRead;
                }
                zipStream.Close();
                bHtml = new byte[offset];
                deBuffer.CopyTo(bHtml, offset);
            }
            else
            {
                bHtml = new byte[offset];
                for (int i = 0; i < offset; i++)
                {
                    bHtml[i] = zipBuffer[i];
                }
            }
            req.Abort();
            resp.Close();
            return(bHtml);
        }
Ejemplo n.º 2
0
        public static string GetPost(RequestArgs mReq)
        {
            string         sHtml = "";
            HttpWebRequest req   = (HttpWebRequest)HttpWebRequest.Create(mReq.Url);

            req.Method = mReq.Method;
            req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*";
            if (string.IsNullOrEmpty(mReq.charset))
            {
                req.ContentType = "application/x-www-form-urlencoded";
            }
            else
            {
                req.ContentType = "application/x-www-form-urlencoded; charset=" + mReq.charset;
            }
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            if (mReq.cookie != string.Empty)
            {
                req.Headers.Add(HttpRequestHeader.Cookie, mReq.cookie);
            }
            req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            req.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn");
            req.KeepAlive = true;

            if (mReq.RefererUrl != "")
            {
                req.Referer = mReq.RefererUrl;
            }
            req.AllowAutoRedirect = mReq.blRedirect;
            if (mReq.blPolicy == true)
            {
                System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            }
            if (mReq.Method == "POST")
            {
                StreamWriter sw = null;
                sw = new StreamWriter(req.GetRequestStream());
                sw.Write(mReq.postData);
                sw.Close();
            }
            Encoding encoding = Encoding.UTF8;

            switch (mReq.Encode.ToUpper())
            {
            case "UTF-8":
                encoding = Encoding.UTF8;
                break;

            case "GB2312":
                encoding = Encoding.GetEncoding("gb2312");
                break;

            case "BIG5":
                encoding = Encoding.GetEncoding("BIG5");
                break;

            default:
                encoding = Encoding.GetEncoding("gb2312");
                break;
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream          s    = resp.GetResponseStream();

            if (resp.ContentEncoding == "gzip")
            {
                byte[] zipBuffer = new byte[1024 * 100];
                int    bytesRead = 0;
                int    offset    = 0;
                while (true)
                {
                    bytesRead = s.Read(zipBuffer, offset, zipBuffer.Length - offset);
                    if (bytesRead <= 0)
                    {
                        break;
                    }
                    offset += bytesRead;
                }
                MemoryStream ms        = new MemoryStream(zipBuffer, 0, offset, true);
                GZipStream   zipStream = new GZipStream(ms, CompressionMode.Decompress, true);
                ms.Seek(0, SeekOrigin.Begin);
                byte[] deBuffer = new byte[1024 * 300];
                offset = 0;
                while (true)
                {
                    bytesRead = zipStream.Read(deBuffer, offset, 1024);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    offset += bytesRead;
                }
                zipStream.Close();
                sHtml = encoding.GetString(deBuffer, 0, offset);
            }
            else
            {
                StreamReader sr = new StreamReader(s, encoding);
                sHtml = sr.ReadToEnd();
                sr.Close();
            }
            string sHeader = "";

            if (mReq.blGetHeaders == true)
            {
                foreach (string header in resp.Headers)
                {
                    sHeader += header + ":" + resp.Headers[header] + "\r\n";
                }
            }
            sHtml = sHeader + sHtml;
            req.Abort();
            resp.Close();
            return(sHtml);
        }
Ejemplo n.º 3
0
        public static string GetPost(RequestArgs mArgs)
        {
            //(1)创建IPEndPoint实例和套接字
            IPAddress  hostIp = Dns.GetHostEntry(mArgs.IpAddress).AddressList[0];
            IPEndPoint ep     = new IPEndPoint(hostIp, 80);
            Socket     client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //(2)连接服务器端
            try
            {
                client.Connect(ep);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.Message);
            }

            StringBuilder sbRequest = new StringBuilder();
            Uri           u         = new Uri(mArgs.Url);

            sbRequest.AppendLine(string.Format("{0} {1} HTTP/1.1", mArgs.Method, u.PathAndQuery));



            if (string.IsNullOrEmpty(mArgs.Accept))
            {
                sbRequest.AppendLine("Accept: image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            }
            else
            {
                sbRequest.AppendLine(string.Format("Accept: {0}", mArgs.Accept));
            }


            sbRequest.AppendLine("Accept-Language: zh-cn");
            sbRequest.AppendLine("User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30618; InfoPath.2)");
            sbRequest.AppendLine("Accept-Encoding: gzip, deflate");
            sbRequest.AppendLine(string.Format("Host: {0}", u.Host));
            if (!string.IsNullOrEmpty(mArgs.RefererUrl))
            {
                sbRequest.AppendLine(string.Format("Referer: {0}", mArgs.RefererUrl));
            }

            if (!string.IsNullOrEmpty(mArgs.ContentType))
            {
                sbRequest.AppendLine(string.Format("Content-Type: {0}", mArgs.ContentType));
            }

            sbRequest.AppendLine("Connection: Keep-Alive");
            if (mArgs.Method == "POST")
            {
                sbRequest.AppendLine(string.Format("Content-Length: {0}", mArgs.postData.Length));
            }
            sbRequest.AppendLine("");

            if (mArgs.Method == "POST")
            {
                sbRequest.AppendLine(mArgs.postData);
                sbRequest.AppendLine("");
            }

            //(3)发送请求
            client.Send(Encoding.ASCII.GetBytes(sbRequest.ToString()));

            //(4)接收数据
            StringBuilder recstr = new StringBuilder();

            byte[] buff = new byte[1024 * 3];

            int rCount = 0;

            while (true)
            {
                rCount = client.Receive(buff, buff.Length, SocketFlags.None); //读取数据
                if (rCount > 0)
                {
                    recstr.Append(Encoding.GetEncoding(mArgs.Encode).GetString(buff, 0, rCount));
                    System.Threading.Thread.Sleep(50);
                }
                if (rCount <= buff.Length)
                {
                    break;
                }
            }
            client.Close();
            if (mArgs.blGetHeaders)
            {
                return(recstr.ToString());
            }
            else
            {
                return(CRegex.Replace(recstr.ToString(), @"^HTTP[\s\S]+?(\r\n){2,}", "", 0));
            }
        }