• HttpHelpers类普通GET和POST方式,带Cookie和带证书验证模式


    /// <summary>
    /// 类说明:HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式
    /// 编码日期:2011-09-13
     
    /// </summary>using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Security.Cryptography.X509Certificates;
    using System.Net.Security;
    using System;
      
    /// <summary>
    ///HttpHelpers 主要是实现Http方式的Get和Post方式请求
    /// </summary>
    public class HttpHelpers
    {
        /// <summary>
        /// 构造器实现默认属性的赋值
        /// </summary>
        public HttpHelpers()
        {
            //设置请求方式为Post
            request.Method = "GET";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/x-www-form-urlencoded";
        }
      
        #region 所有的属性
      
        //访问的页面地址
        private string RequestURl { get; set; }
      
        //默认的编码
        private Encoding encoding { get; set; }
      
        //HttpWebRequest对象用来发起请求
        private HttpWebRequest request { get; set; }
      
        //获取影响流的数据对象
        private HttpWebResponse response { get; set; }
      
        //证书文件X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer");
        X509Certificate objx509 { get; set; }
      
        //请求方式目前只提供Post和Get方式
        private string Method { get; set; }
      
        //Accept属性
        private string Accept { get; set; }
      
        //ContentType属性
        private string ContentType { get; set; }
      
        //UserAgent属性
        private string UserAgent { get; set; }
      
        //Cookie列表
        private CookieContainer cookie { get; set; }
      
        //需要返回的数据对象
        private string reutrnDate { get; set; }
      
        //Post数据串
        private string strPostdata { get; set; }
      
        #endregion
      
        #region 内部方法
      
        //回调验证证书问题
        private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            // 总是接受    
            return true;
        }
      
        /// <summary>
        /// 根据相传入的数据,得到相应页面数据
        /// </summary>
        /// <param name="strPostdata">传入的数据Post方式,get方式传NUll或者空字符串都可以</param>
        /// <returns>string类型的响应数据</returns>
        private string GetHttpRequestData()
        {
            try
            {
                //是否要添加证书验证
                if (objx509 != null)
                {
                    //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
                    ServicePointManager.ServerCertificateValidationCallback =
                        new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                }
      
                //创建request对象
                request = (HttpWebRequest)WebRequest.Create(RequestURl);
      
                //是否要添加证书验证
                if (objx509 != null)
                    request.ClientCertificates.Add(objx509);
      
                //是否带有Cookie值
                if (cookie != null)
                    request.CookieContainer = cookie;
      
                //当为post提交是需要填充数据
                if (Method.Trim().ToLower() == "post")
                {
                    byte[] buffer = encoding.GetBytes(strPostdata);
                    request.ContentLength = buffer.Length;
                    request.GetRequestStream().Write(buffer, 0, buffer.Length);
                }
      
                //得到请求的response
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    //从这里开始我们要无视编码了
                    if (encoding == null)
                        GetEonding();
      
                    //开始读取流并设置编码方式
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        //从当前开始读取整个流数据,默认为0所以读取的是全部,并返回数据
                        reutrnDate = reader.ReadToEnd().ToString().Trim();
                    }
                }
            }
            catch (Exception)
            {
                //这里是在发生异常时返回的错误信息
                reutrnDate = "String Error";
            }
            return reutrnDate;
        }
      
        /// <summary>
        /// 得到response对象的编码类型
        /// </summary>
        private void GetEonding()
        {
            if (response.CharacterSet.Trim() != "")
                encoding = System.Text.Encoding.GetEncoding(response.CharacterSet.Trim());
            else
                encoding = System.Text.Encoding.UTF8;
        }
      
        #endregion
      
        #region 公开方法
      
        /// <summary>
        /// 只设置一些简单参数的方式
        /// </summary>
        /// <param name="_url">URl地址</param>
        /// <param name="_strPostdata">Post请求方式时传入的数据</param>
        /// <param name="_Method">请求方式GET或者POST可以为空默认为GET</param>
        /// <param name="_encoding">编码方式可以为空默认为UTF-8</param>
        /// <param name="_Accept">Accept属性</param>
        /// <param name="_ContentType">ContentType属性</param
        /// <param name="_UserAgent">UserAgent属性</param
        /// <param name="_cookie">CookieContainer列表</param
        /// <param name="_objx509">X509Certificate证书对象</param
        /// <returns>请求所得到的数据</returns>
        public string GetString_null(string _url, string _strPostdata, string _Method, Encoding _encoding, string _Accept,
            string _ContentType, string _UserAgent, CookieContainer _cookie, X509Certificate _objx509)
        {
            RequestURl = _url;
            Method = _Method;
            encoding = _encoding;
            Accept = _Accept;
            ContentType = _ContentType;
            UserAgent = _UserAgent;
            cookie = _cookie;
            objx509 = _objx509;
            return GetHttpRequestData();
        }
      
        /// <summary>
        /// 只设置一些简单参数的方式
        /// </summary>
        /// <param name="_url">URl地址</param>
        /// <param name="_strPostdata">Post请求方式时传入的数据</param>
        /// <param name="_Method">请求方式GET或者POST可以为空默认为GET</param>
        /// <param name="_encoding">编码方式可以为空默认为UTF-8</param>
        /// <returns>请求所得到的数据</returns>
        public string GetString_type(string _url, string _Method, Encoding _encoding)
        {
            RequestURl = _url;
            Method = _Method;
            encoding = _encoding;
            return GetHttpRequestData();
        }
      
        //下面大家自己可以多写几种常用的,呵呵
      
        #endregion
      
    }

      

  • 相关阅读:
    正则表达式
    寒假第三天
    起航
    写正则表达式心得(一)
    80后偷偷“变老”的20种表现
    数据库三大范式
    给从表添加主键。数据库备份方法
    appletouchicon 和 appletouchiconprecomposed的区别
    一些对前端开发有用的网站
    背景图可以随时自动调整大小jquery插件backstretch
  • 原文地址:https://www.cnblogs.com/splendidme/p/2175364.html
Copyright © 2020-2023  润新知