小周的技术博客

困难是培养伟大心志的保姆,唯有这个冷酷的保姆才会不停地推着摇篮,培养一个勇敢、刚健的孩子。 ---------布赖恩特

HttpHelper用于HttpWebRequest和HttpWebResponse的封装类

本文来自 小周的技术博客 转载请注明;

本文地址:http://blog.lyttensoft.win/?id=62



此类可以用于Http的GET和POST的数据交互,用于场景比如模拟登录

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.IO.Compression;

namespace System.Http
{
    public class HttpHelper
    {
        public HttpWebRequest httpWebRequest;
        public HttpWebResponse HttpWebResponse;
        /// <summary>
        /// HttpResponseModel对象。其中ResponsStream对象用完,记得close
        /// </summary>
        /// <param name="ParameterModel"></param>
        /// <returns></returns>
        public HttpResponseModel Http(HttpParameterModel ParameterModel)
        {
            httpWebRequest = (HttpWebRequest)WebRequest.Create(ParameterModel.Url);

            #region 请求基本参数
            httpWebRequest.Accept = ParameterModel.Accept;
            httpWebRequest.Method = ParameterModel.Method;
            httpWebRequest.Referer = ParameterModel.Referer;
            httpWebRequest.UserAgent = ParameterModel.UserAgent;
            httpWebRequest.ContentType = httpWebRequest.ContentType;
            #endregion

            #region formData参数
            if (ParameterModel.IsformData && ParameterModel.formData != null)
            {
                string formdatastr = "";
                StringBuilder sb = new StringBuilder();
                foreach (var item in ParameterModel.formData)
                {
                    sb.AppendFormat("&{0}={1}", item.Key, item.Value);
                }
                formdatastr = sb.ToString().Substring(1);
                byte[] byteArray = ParameterModel.RequestEncodeing.GetBytes(formdatastr);
                httpWebRequest.ContentLength = byteArray.Length;
                Stream newStream = httpWebRequest.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();
            }
            #endregion

            #region 请求Header头参数
            if (ParameterModel.IsHeader)
            {
                httpWebRequest.Headers = ParameterModel.webHeaderCollection;
            }
            #endregion

            #region 获取返回流,如果zip,则解压
            HttpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = HttpWebResponse.GetResponseStream();
            //如果http头中接受gzip的话,这里就要判断是否为有压缩,有的话,直接解压缩即可  
            if (HttpWebResponse.Headers["Content-Encoding"] != null && HttpWebResponse.Headers["Content-Encoding"].ToLower().Contains("gzip"))
                responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
            #endregion

            #region 设置返回参数
            HttpResponseModel HttpResponseModel = new HttpResponseModel();
            if (ParameterModel.IsResponsStream)
            {
                HttpResponseModel.ResponsStream = responseStream;
            }
            if (ParameterModel.IsResponsWebHeaders)
            {
                HttpResponseModel.ResponsWebHeaders = httpWebRequest.Headers;
            }
            if (ParameterModel.IsResponsHtml)
            {
                StreamReader reader = new StreamReader(responseStream, ParameterModel.ResponseEncodeing);
                HttpResponseModel.ResponsHtml = reader.ReadToEnd().ToString();
            }
            #endregion
            return HttpResponseModel;
        }
    }

    public class HttpParameterModel
    {
        public string Url { get; set; }
        public string Referer { get; set; }
        public string Method { get; set; }
        public string Accept { get; set; }
        public string UserAgent { get; set; }
        public string ContentType { get; set; }
        /// <summary>
        /// 返回时封装的编码
        /// </summary>
        public Encoding ResponseEncodeing { get; set; }
        /// <summary>
        /// 请求时封装的编码
        /// </summary>
        public Encoding RequestEncodeing { get; set; }
        private bool _IsformData = false;
        public bool IsformData { get { return _IsformData; } set { _IsformData = value; } }
        public Dictionary<string, string> formData { get; set; }
        public bool IsHeader { get; set; }
        public WebHeaderCollection webHeaderCollection { get; set; }

        private bool _IsResponsStream = false;
        public bool IsResponsStream { get { return _IsResponsStream; } set { _IsResponsStream = value; } }
        private bool _IsResponsHtml = false;
        public bool IsResponsHtml { get { return _IsResponsHtml; } set { _IsResponsHtml = value; } }
        private bool _IsResponsWebHeaders = false;
        public bool IsResponsWebHeaders { get { return _IsResponsWebHeaders; } set { _IsResponsWebHeaders = value; } }
    }

    /// <summary>
    /// 返回对象
    /// </summary>
    public class HttpResponseModel
    {

        public Stream ResponsStream { get; set; }

        public string ResponsHtml { get; set; }

        public WebHeaderCollection ResponsWebHeaders { get; set; }
    }

    public class HttpConstParameter
    {
        /// <summary>
        /// text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
        /// </summary>
        public const string Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";

        /// <summary>
        /// Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
        /// </summary>
        public const string UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
        /// <summary>
        /// application/x-www-form-urlencoded
        /// </summary>
        public const string ContentType = "application/x-www-form-urlencoded";
        /// <summary>
        /// gzip,deflate
        /// </summary>
        public const string AcceptEncoding = "gzip,deflate";
        /// <summary>
        /// zh-CN,zh;q=0.8
        /// </summary>
        public const string AcceptLanguage = "zh-CN,zh;q=0.8";
        /// <summary>
        /// max-age=0
        /// </summary>
        public const string CacheControl = "max-age=0";
        /// <summary>
        /// 1
        /// </summary>
        public const string Upgrade = "1";
        /// <summary>
        /// GBK,utf-8;q=0.7,*;q=0.3
        /// </summary>
        public const string AcceptCharset = "GBK,utf-8;q=0.7,*;q=0.3";
        /// <summary>
        /// no-referrer-when-downgrade
        /// </summary>
        public const string Referer = "no-referrer-when-downgrade";
    }
}


调用实例

 static void Main(string[] args)
        {

            //在登录页面获取token
            System.Http.HttpHelper http = new System.Http.HttpHelper();
            HttpParameterModel model = new HttpParameterModel();
            model.Accept = HttpConstParameter.Accept;
            model.ContentType = HttpConstParameter.ContentType;
            model.Method = "GET";
            model.Referer = HttpConstParameter.Referer;
            model.Url = "https://accounts.ctrip.com/member/login.aspx";
            model.IsResponsHtml = true;
            HttpResponseModel respones = http.Http(model);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(respones.ResponsHtml);
            Dictionary<string, string> dics = Getformdata(doc);



        }




本文来自 小周的技术博客 转载请注明;

本文地址:http://blog.lyttensoft.win/?id=62

发表评论:

小周的技术博客