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[ex.Response.ContentLength];
                    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);
        }
        /// <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. 3
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[ex.Response.ContentLength];
                    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);
        }
        /// <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;
        }