[C#] 纯文本查看 复制代码
using RKRG.Utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace RKRG.Utils
{
    class Common
    {
        #region 全局变量部分
        //http请求参数
        //用户的浏览器类型,版本,操作系统
        string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0";
        //返回类型
        string contentType = "application/x-www-form-urlencoded";
        //是否根据301跳转
        bool allowautoredirect = false;
        //连接超时时间
        int timeout = 10000;
        //写入Post数据超时时间
        int readWriteTimeout = 100000;
        //创建Http访问类对象
        HttpHelper http = new HttpHelper();
        public CookieCollection cc_gb = new CookieCollection();
        public string location = ""; 
        
        public string htmlContent = "";
        string cookieStr = "";
        #endregion
        #region httpMethod
        /// <summary>
        /// post方法的http请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="referer"></param>
        /// <param name="postDate"></param>
        public string postMethod_HttpRquest(string url, string refer, string postData)
        {
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = url,   
                Method = "post",   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写                   
                Referer = refer,//来源URL     可选项   
                Postdata = postData,//Post数据     可选项GET时不需要写   
                Timeout = timeout,//连接超时时间     可选项默认为100000 
                Encoding = Encoding.UTF8,
                ReadWriteTimeout = readWriteTimeout,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = userAgent,//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = contentType,//返回类型    可选项有默认值   
                Allowautoredirect = allowautoredirect,//是否根据301跳转     可选项   
                ResultCookieType = ResultCookieType.CookieCollection,
                CookieCollection = cc_gb,
                KeepAlive = false
                //Cookie = cookieStr
            };
            HttpResult result = http.GetHtml(item);
            if (result != null)
            {
                if (result.CookieCollection != null)
                {
                    for (int i = 0; i < result.CookieCollection.Count; i++)
                    {
                        cc_gb.Add(result.CookieCollection[i]);
                    }
                    cookieStr = result.CookieCollection.ToString();
                }
                //if (result.Cookie != null)
                //{
                //    cookieStr = result.Cookie;
                //}
                WebHeaderCollection header = result.Header;
                if (header != null)
                {
                    if (header.GetValues("Location") != null)
                    {
                        this.location = header.GetValues("Location")[0];
                    }
                    else
                    {
                        this.location = "";
                    }
                }
                return result.Html;
            }
            return "";
        }
        /// <summary>
        /// get方法的http请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="referer"></param>
        /// <param name="postDate"></param>
        public string getMethod_HttpRquest(string url, string refer)
        {
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = url,   
                Method = "get",   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写                   
                Referer = refer,//来源URL     可选项   
                Timeout = timeout,//连接超时时间     可选项默认为100000    
                ReadWriteTimeout = readWriteTimeout,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = userAgent,//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = contentType,//返回类型    可选项有默认值   
                Allowautoredirect = allowautoredirect,//是否根据301跳转     可选项   
                //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数   
                //Connectionlimit = 1024,//最大连接数     可选项 默认为1024    
                //ProxyIp = "",//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数    
                //ProxyPwd = "123456",//代理服务器密码     可选项    
                //ProxyUserName = "administrator",//代理服务器账户名     可选项   
                ResultCookieType = ResultCookieType.CookieCollection,
                CookieCollection = cc_gb,
                KeepAlive = true
                //Cookie = cookieStr
            };
            HttpResult result = http.GetHtml(item);
            if (result != null)
            {
                if (result.CookieCollection != null)
                {
                    for (int i = 0; i < result.CookieCollection.Count; i++)
                    {
                        cc_gb.Add(result.CookieCollection[i]);
                    }
                }
                //if (result.Cookie != null)
                //{
                //    cookieStr = result.Cookie;
                //}
                WebHeaderCollection header = result.Header;
                this.htmlContent = result.Html;
                if (header != null)
                {
                    if (header.GetValues("Location") != null)
                    {
                        this.location = header.GetValues("Location")[0];
                    }
                    else
                    {
                        this.location = "";
                    }
                }
                return result.Html;
            }
            return "";
        }
        /// <summary>
        /// 获取图片
        /// </summary>
        public Image getImage(string url, string referer)
        {
            HttpItem item = new HttpItem()
            {
                URL = url,
                Method = "GET",
                Accept = "image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5",
                Referer = referer,//来源URL     可选项   
                Timeout = timeout,//连接超时时间     可选项默认为100000    
                ReadWriteTimeout = readWriteTimeout,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = userAgent,//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = contentType,//返回类型    可选项有默认值   
                Allowautoredirect = allowautoredirect,//是否根据301跳转     可选项 
                //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数   
                //Connectionlimit = 1024,//最大连接数     可选项 默认为1024    
                //ProxyIp = this.proxy,//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数    
                //ProxyPwd = "123456",//代理服务器密码     可选项    
                //ProxyUserName = "administrator",//代理服务器账户名     可选项   
                ResultType = ResultType.Byte,
                //ResultCookieType = ResultCookieType.CookieCollection,
                //CookieCollection = cc_gb
                KeepAlive = true,
                Cookie = cookieStr
            };
            HttpResult result = http.GetHtml(item);
            //if (result.CookieCollection != null)
            //{
            //    cc_gb.Add(result.CookieCollection);
            //}
            if (result.Cookie != null)
            {
                cookieStr = result.Cookie;
            }
            MemoryStream ms = new MemoryStream(result.ResultByte);
            Image img = Image.FromStream(ms);
            //流用完要及时关闭  
            ms.Close();
            return img;  //得到验证图片
        }
        #endregion
    }
}