Esempio n. 1
0
 /// <summary>
 /// Http请求头部处理
 /// </summary>
 /// <param name="httpRequest"></param>
 protected override void RequestHeaderExecuting(HttpWebClient httpRequest)
 {
     if (!string.IsNullOrEmpty(this.Token.AccessToken))
     {
         httpRequest.Headers["Authorization"] = string.Format("OAuth2 {0}", this.Token.AccessToken);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 发送Http请求
        /// </summary>
        /// <param name="url">请求Url</param>
        /// <param name="method">请求类型</param>
        /// <param name="parameters">参数集合</param>
        /// <returns></returns>
        private String Request(string url, RequestMethod method = RequestMethod.Get, params RequestOption[] parameters)
        {
            HttpWebClient         httpWebClient  = new HttpWebClient();
            HttpWebResponseResult responseResult = null;

            #region 发起Http数据请求
            switch (method)
            {
            case RequestMethod.Get:
            {
                #region         //请求头和请求参数预处理
                RequestHeaderExecuting(httpWebClient);
                parameters = RequestGetExecuting(parameters);
                #endregion

                #region         //传递参数
                foreach (var item in parameters)
                {
                    httpWebClient.SetField(item.Name, (string)item.Value);
                }
                #endregion

                //发起请求
                responseResult = httpWebClient.Get(url);
            }
            break;

            case RequestMethod.Post:
            {
                #region         //请求头和请求参数预处理
                RequestHeaderExecuting(httpWebClient);
                parameters = RequestPostExecuting(parameters);
                #endregion

                #region         //判断当前POST是否为Multipart(即同时包含普通表单字段和文件表单字段)
                bool isMultipart = false;
                foreach (var item in parameters)
                {
                    if (item.IsBinary)
                    {
                        isMultipart = true;
                        break;
                    }
                }
                #endregion

                #region         //传递参数
                if (isMultipart)
                {
                    foreach (var item in parameters)
                    {
                        if (item.IsBinary)
                        {
                            httpWebClient.SetField(item.Name, (byte[])item.Value, String.Empty, String.Empty);
                        }
                        else
                        {
                            httpWebClient.SetField(item.Name, (string)item.Value, true);
                        }
                    }
                }
                else
                {
                    foreach (var item in parameters)
                    {
                        httpWebClient.SetField(item.Name, (string)item.Value);
                    }
                }
                #endregion

                //发起请求
                responseResult = httpWebClient.Post(url);
            }
            break;
            }
            #endregion

            return(responseResult.IsSuccess ? responseResult.ResponseText : String.Empty);
        }
Esempio n. 3
0
 protected virtual void RequestHeaderExecuting(HttpWebClient httpRequest)
 {
 }