示例#1
0
 /// <summary>
 /// 设置WebRequest请求证书信息
 /// </summary>
 /// <param name="requestItem"></param>
 protected void SetCertificateParams(RequestItem requestItem)
 {
     if (requestItem.IsNeedCertificate)
     {
         ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
     }
     WebRequest = System.Net.WebRequest.Create(requestItem.RequestUrl) as HttpWebRequest;
 }
示例#2
0
 /// <summary>
 /// 设置HTTP请求代理
 /// </summary>
 /// <param name="requestItem"></param>
 protected void SetWebProxyParams(RequestItem requestItem)
 {
     if (null != requestItem.ProxyInfo)
     {
         ProxySetInfo proxyInfo = requestItem.ProxyInfo;
         if (!string.IsNullOrEmpty(proxyInfo.ProxyIp))
         {
             WebProxy webProxy = new WebProxy(proxyInfo.ProxyIp);
             webProxy.Credentials   = new NetworkCredential(proxyInfo.ProxyUserName, proxyInfo.ProxyPassword);
             WebRequest.Proxy       = webProxy;
             WebRequest.Credentials = CredentialCache.DefaultCredentials;   //设置安全凭证
         }
     }
 }
示例#3
0
        /// <summary>
        /// 使用POST方式请求数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="requestItem"></param>
        /// <param name="responseCallBack"></param>
        /// <returns></returns>
        public T RequestDataByPost <T>(RequestItem requestItem, Func <HttpWebResponse, T> responseCallBack)
        {
            T result = default(T);

            if (null != requestItem)
            {
                requestItem.Method = HttpMethodEnum.Post;
                result             = CreateWebRequest(requestItem, responseCallBack);
            }
            else
            {
                throw new ArgumentNullException("POST请求-请求参数RequestItem为Null");
            }
            return(result);
        }
示例#4
0
 /// <summary>
 /// 设置POST请求参数
 /// </summary>
 /// <param name="requestItem"></param>
 protected void SetPostRequestParams(RequestItem requestItem)
 {
     if (WebRequest.Method.ToLower() == "post")
     {
         if (string.IsNullOrEmpty(WebRequest.ContentType))
         {
             WebRequest.ContentType = "application/x-www-form-urlencoded";
         }
         WebRequest.ReadWriteTimeout = requestItem.ReadWriteTimeout;
         byte[] buffer = Encoding.UTF8.GetBytes(requestItem.GetParamsData());
         WebRequest.ContentLength = buffer.Length;
         Stream writer = WebRequest.GetRequestStream();
         writer.Write(buffer, 0, buffer.Length);
         writer.Close();
     }
 }
示例#5
0
        /// <summary>
        /// 创建HttpWebRequest请求
        /// </summary>
        /// <typeparam name="T">返回数据类型</typeparam>
        /// <param name="requestItem">请求参数</param>
        /// <param name="responseCallBack"></param>
        /// <returns></returns>
        public T CreateWebRequest <T>(RequestItem requestItem, Func <HttpWebResponse, T> responseCallBack)
        {
            T result = default(T);

            if (null == requestItem)
            {
                throw new ArgumentNullException("请求参数RequestItem为Null");
            }
            if (responseCallBack == null)
            {
                throw new ArgumentNullException("委托responseCallBack为Null");
            }
            try
            {
                SetCertificateParams(requestItem);
                if (null != WebRequest)
                {
                    SetWebProxyParams(requestItem);                    //设置代理
                    SetServicePointParams();                           //设置请求的服务端信息
                    SetRequestParams(requestItem);                     // 设置HTTP请求头,HTTP版本信息等信息
                    SetPostRequestParams(requestItem);                 //设置POST请求参数
                        result = GetWebResponse(responseCallBack);     //获取WebResponse响应
                }
                else
                {
                    throw new ArgumentNullException("创建HttpWebRequest请求失败 HttpWebRequest为Null");
                }
            }
            catch (Exception ex)
            {
                //OutLog("代理服务器拒绝握手,超小气。。。");
                //OutLog("HttpRequest高速缓存池出现致命错误:");
                //OutLog(ex.Message);
                return(default(T));
            }
            finally
            {
                CloseWebResponse();      //关闭响应
                AbortWebRequest();       //取消请求
            }
            return(result);
        }
示例#6
0
        /// <summary>
        ///  设置HTTP请求头,HTTP版本信息等信息
        /// </summary>
        /// <param name="requestItem"></param>
        protected void SetRequestParams(RequestItem requestItem)
        {
            WebRequest.Method = requestItem.Method.ToString();            //请求类型
            WebRequest.ProtocolVersion = HttpVersion.Version11;           //请求HTTP版本
            WebRequest.Timeout         = requestItem.Timeout;             //超时时间

            if (requestItem.RequestCookies == null)
            {
                requestItem.RequestCookies = new CookieContainer();
            }
            WebRequest.CookieContainer = requestItem.RequestCookies;        //设置与请求关联的Cookies

            WebRequest.KeepAlive         = requestItem.KeepAlive;           //是否保持持久链接
            WebRequest.AllowAutoRedirect = requestItem.AutoRedirect;        //是否允许自动跳转
            if (!string.IsNullOrEmpty(requestItem.Referer))
            {
                WebRequest.Referer = requestItem.Referer;    //请求来源Url
            }
            if (!string.IsNullOrEmpty(requestItem.ContentType))
            {
                WebRequest.ContentType = requestItem.ContentType;
            }
            if (!string.IsNullOrEmpty(requestItem.RequestAccept))
            {
                WebRequest.Accept = requestItem.RequestAccept;     //设置Accept标头
            }
            if (string.IsNullOrEmpty(requestItem.UserAgent))
            {
                requestItem.UserAgent = UserAgentIe;
            }
            WebRequest.UserAgent = requestItem.UserAgent;   //设置User-Agent标头

            WebHeaderCollection headerColl = requestItem.GetHeaderData();

            if (null != headerColl && headerColl.Count > 0)
            {
                WebRequest.Headers.Add(headerColl);
            }
        }
示例#7
0
 /// <summary>
 ///  使用POST方式请求数据
 /// </summary>
 /// <param name="requestItem"></param>
 /// <returns></returns>
 public ResponseItem RequestDataByPost(RequestItem requestItem)
 {
     return(RequestDataByPost <ResponseItem>(requestItem, ResponseCallBack));
 }