HTTP原型

时间:2023-12-31 23:59:20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Drawing;

namespace HTTPHelpers
{
    class http
    {

        // 公用Cookie信息
        public static CookieContainer myCookie;
        //返回的Html代码或结果
        public static string html;
        /// <summary>
        /// Get方式提交
        /// </summary>
        /// <param name="strUrl">目的资源链接</param>
        /// <returns>String:Response返回值</returns>
        public static string GetModel(string strUrl, string server)
        {
            string strRet = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
                request.Timeout = ; HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                System.IO.Stream resStream = response.GetResponseStream();
                Encoding encode = System.Text.Encoding.UTF8;
                StreamReader readStream = new StreamReader(resStream, encode);
                Char[] read = ];
                , );
                )
                {
                    String str = , count);
                    strRet = strRet + str;
                    count = readStream.Read(read, , );
                }
                resStream.Close();
            }
            catch (Exception e)
            {
                strRet = "";
            }
            return strRet;
        }

        /// <summary>
        /// 绑定Cookie、获取验证码
        /// </summary>
        /// <param name="strUrl">链接地址(验证码获取地址)</param>
        /// <param name="server">Referer地址</param>
        /// <returns>Stream:验证码的数据流</returns>
        public static Stream GetModelStream(string strUrl, string server)//, string proxy, bool isProxy)
        {
            System.IO.Stream resStream = null;
            string cookie;
            try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                httpRequest.Timeout = ;
                httpRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpRequest.Referer = server;
                httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                cookie = httpResponse.Headers.Get("Set-Cookie");
                myCookie = new CookieContainer();
                myCookie.SetCookies(new Uri(strUrl), cookie);
                resStream = httpResponse.GetResponseStream();
            }
            catch { }
            return resStream;
        }

        /// <summary>
        /// Post方法提交
        /// </summary>
        /// <param name="strUrl">目的资源链接</param>
        /// <param name="strParm">传递的参数和值</param>
        /// <returns>String:Response返回值</returns>
        public static string PostModel(string strUrl, string server, string strParm)
        {
            Encoding encode = System.Text.Encoding.UTF8;
            byte[] arrB = encode.GetBytes(strParm);
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
            myReq.Method = "POST";
            myReq.ContentType = "application/x-www-form-urlencoded";
            myReq.Timeout = ;
            myReq.Referer = server;
            myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
            myReq.ContentLength = arrB.Length;
            myReq.CookieContainer = myCookie;
            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, , arrB.Length);
            outStream.Close();
            WebResponse myResp = null;
            try
            {
                //接收HTTP做出的响应
                myResp = myReq.GetResponse();
            }
            catch (Exception e)
            {
            }
            Stream ReceiveStream = myResp.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream, encode);
            Char[] read = ];
            , );
            string str = null;
            )
            {
                str += , count);
                count = readStream.Read(read, , );
            }
            readStream.Close();
            myResp.Close();
            return str;
        }

    }
}