Ejemplo n.º 1
0
        private void ProcessError(HttpHelperReturn ret, WebException ex)
        {
            if (ex.Status == WebExceptionStatus.Timeout)
            {
                var time = this.Timeout == 0 ? 10 : this.Timeout;
                throw new Exception("请求接口超时,超时时间为" + time + "秒");
            }
            HttpWebResponse response = (HttpWebResponse)ex.Response;

            if (response != null)
            {
                ret.StatusCode = (int)response.StatusCode;
                try
                {
                    using (Stream data = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(data))
                        {
                            ret.Return = reader.ReadToEnd();
                        }
                    }
                }
                catch { };
            }
            else
            {
                throw ex;
            }
        }
Ejemplo n.º 2
0
        public HttpHelperReturn PutRequest(string LoginUrl)
        {
            var ret = new HttpHelperReturn();

            try
            {
                request       = (HttpWebRequest)WebRequest.Create(LoginUrl);
                request.Proxy = null;
                request.ServicePoint.ConnectionLimit = 1024;
                if (this.Timeout > 0)
                {
                    request.Timeout = this.Timeout * 1000;
                }
                byte[] data = Encoding.GetBytes(RequestData);
                foreach (var header in this.Headers)
                {
                    request.Headers.Add(header);
                }
                foreach (var header in DHeaders)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
                request.Method        = "PUT";
                request.ContentType   = this.ContentType;
                request.ContentLength = data.Length;
                request.KeepAlive     = false;
                request.UserAgent     = UserAgent;
                stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                response       = (HttpWebResponse)request.GetResponse();
                ret.StatusCode = (int)response.StatusCode;
                streamReader   = new StreamReader(response.GetResponseStream(), Encoding);
                ret.Return     = streamReader.ReadToEnd();
            }
            catch (WebException ex)
            {
                ProcessError(ret, ex);
            }
            finally
            {
                Close();
            }
            return(ret);
        }
Ejemplo n.º 3
0
        public HttpHelperReturn GetRequest(string LocationUrl)
        {
            var ret = new HttpHelperReturn();

            try
            {
                request       = (HttpWebRequest)WebRequest.Create(LocationUrl);
                request.Proxy = null;
                request.ServicePoint.ConnectionLimit = 1024;
                if (this.Timeout > 0)
                {
                    request.Timeout = this.Timeout * 1000;
                }
                foreach (var header in this.Headers)
                {
                    request.Headers.Add(header);
                }
                foreach (var header in DHeaders)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
                request.Method    = "GET";
                request.KeepAlive = false;
                request.UserAgent = UserAgent;
                response          = (HttpWebResponse)request.GetResponse();
                stream            = response.GetResponseStream();
                streamReader      = new StreamReader(stream, Encoding);
                ret.StatusCode    = (int)response.StatusCode;
                ret.Return        = streamReader.ReadToEnd();
            }
            catch (WebException ex)
            {
                ProcessError(ret, ex);
            }
            finally
            {
                Close();
            }
            return(ret);
        }