Esempio n. 1
0
        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="requestUrl">请求url</param>
        /// <param name="responseText">响应</param>
        /// <returns></returns>
        public HttpWebResponseResult Get(String requestUrl)
        {
            this.isPost            = false;
            this.requestUrl        = requestUrl;
            this.webResponseResult = new HttpWebResponseResult();

            webClient       = new WebClient();
            webClient.Proxy = null;

            #region //请求头部处理
            requestHeads.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
            foreach (var key in requestHeads.AllKeys)
            {
                webClient.Headers.Add(key, requestHeads.Get(key));
            }
            #endregion

            #region //请求数据
            byte[] responseBytes = null;
            String url           = GetFields();
            #endregion

            #region //发送请求
            try
            {
                responseBytes = webClient.DownloadData(url);
                webResponseResult.IsSuccess = true;
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    Stream responseStream = ex.Response.GetResponseStream();
                    responseBytes = new byte[responseStream.Length];
                    responseStream.Read(responseBytes, 0, responseBytes.Length);
                }
                else
                {
                    String errorMessage = ex.InnerException.Message;
                    responseBytes = encoding.GetBytes(errorMessage);
                }
                webResponseResult.IsSuccess = false;
            }
            #endregion

            webResponseResult.ResponseText     = System.Text.Encoding.UTF8.GetString(responseBytes);
            webResponseResult.RequestTraceInfo = TraceInfo();

            ResetStatus();

            return(webResponseResult);
        }
Esempio n. 2
0
        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="requestUrl">请求url</param>
        /// <param name="responseText">响应</param>
        /// <returns></returns>
        public HttpWebResponseResult Post(String requestUrl)
        {
            this.isPost            = true;
            this.requestUrl        = requestUrl;
            this.webResponseResult = new HttpWebResponseResult();

            webClient       = new WebClient();
            webClient.Proxy = null;

            #region //请求头部处理
            requestHeads.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
            if (isMultipart)
            {
                requestHeads.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
            }
            else
            {
                requestHeads.Add("Content-Type", "application/x-www-form-urlencoded");
            }

            foreach (var key in requestHeads.AllKeys)
            {
                webClient.Headers.Add(key, requestHeads.Get(key));
            }
            #endregion

            #region //请求数据处理
            byte[] responseBytes;
            byte[] bytes = null;
            if (isMultipart)
            {
                bytes = PostMultipartFields();
            }
            else
            {
                bytes = PostFields();
            }
            #endregion

            #region //发送请求
            try
            {
                responseBytes = webClient.UploadData(this.requestUrl, "POST", bytes);
                webResponseResult.IsSuccess = true;
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    Stream responseStream = ex.Response.GetResponseStream();
                    responseBytes = new byte[responseStream.Length];
                    responseStream.Read(responseBytes, 0, responseBytes.Length);
                }
                else
                {
                    String errorMessage = ex.InnerException.Message;
                    responseBytes = encoding.GetBytes(errorMessage);
                }
                webResponseResult.IsSuccess = false;
            }
            #endregion

            webResponseResult.ResponseText     = System.Text.Encoding.UTF8.GetString(responseBytes);
            webResponseResult.RequestTraceInfo = TraceInfo();

            ResetStatus();

            return(webResponseResult);
        }
Esempio n. 3
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);
        }