Esempio n. 1
0
        public HttpHelper(string domain, string username, string password, string loginurl)
        {
            this._needLogin  = false;
            this._tokenValue = "";
            this._userName   = "";
            if (string.IsNullOrWhiteSpace(domain))
            {
                throw new ArgumentException("domain为空");
            }
            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException("username为空");
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentException("password为空");
            }
            if (string.IsNullOrWhiteSpace(loginurl))
            {
                throw new ArgumentException("loginurl为空");
            }
            string          str  = string.Format("{0}?user={1}&password={2}", loginurl, username, password);
            HttpRequestInfo info = new HttpRequestInfo {
                Domain = domain,
                URI    = str
            };

            this._needLogin  = true;
            this._userName   = username;
            this._tokenValue = this.GetToken(info);
            if (ServicePointManager.DefaultConnectionLimit < 0x200)
            {
                ServicePointManager.DefaultConnectionLimit = 0x200;
            }
        }
Esempio n. 2
0
        public HttpResponseInfo GetResponse(HttpRequestInfo ri)
        {
            string          str;
            HttpWebResponse response = this.GetResponse(ri, out str);

            return(new HttpResponseInfo {
                Response = response, Result = str
            });
        }
Esempio n. 3
0
        private string GetToken(HttpRequestInfo _loginRI)
        {
            string str;

            this.GetResponse(_loginRI, out str);
            if (string.IsNullOrWhiteSpace(str))
            {
                string str2 = Path.Combine(_loginRI.Domain, _loginRI.URI);
                throw new ApplicationException(string.Format("登录失败,{0}", str2));
            }
            return(str);
        }
Esempio n. 4
0
        private HttpWebRequest CreateGetRequest(HttpRequestInfo ri)
        {
            HttpWebRequest request = WebRequest.Create(ri.Domain + "/" + ri.URI) as HttpWebRequest;

            if (ri.Domain.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(this.CheckValidationResult);
                request.ProtocolVersion = HttpVersion.Version10;
            }
            request.Method = "GET";
            this.SetRequest(request, ri);
            return(request);
        }
Esempio n. 5
0
 private void SetRequest(HttpWebRequest request, HttpRequestInfo ri)
 {
     request.AllowAutoRedirect = false;
     request.UserAgent         = ri.UserAgent;
     request.ContentType       = "application/json";
     if (!(!this._needLogin || string.IsNullOrWhiteSpace(this._tokenValue)))
     {
         request.Headers["Token"] = this._tokenValue;
         request.Headers["User"]  = this._userName;
     }
     if (ri.TimeOut > 0)
     {
         request.Timeout = ri.TimeOut * 0x3e8;
     }
 }
Esempio n. 6
0
        private HttpWebRequest CreateRequest(HttpRequestInfo ri)
        {
            if (string.IsNullOrWhiteSpace(ri.URI))
            {
                throw new ArgumentException("请求的URI为空");
            }
            if (string.IsNullOrWhiteSpace(ri.Domain))
            {
                throw new ArgumentException("请求的Domain为空");
            }
            string str = ri.Domain.TrimEnd(new char[] { '/' });

            if (!str.StartsWith("http", StringComparison.OrdinalIgnoreCase))
            {
                ri.Domain = "http://" + str;
            }
            return(string.IsNullOrWhiteSpace(ri.Arguments) ? this.CreateGetRequest(ri) : this.CreatePostRequest(ri));
        }
Esempio n. 7
0
        private HttpWebResponse GetResponse(HttpRequestInfo ri, out string result)
        {
            HttpWebResponse response;
            StreamReader    reader;

            result = null;
            HttpWebRequest request = this.CreateRequest(ri);

            try
            {
                response = request.GetResponse() as HttpWebResponse;
                if (response == null)
                {
                    return(null);
                }
                if (!(!this._needLogin || string.IsNullOrWhiteSpace(response.Headers["Token"])))
                {
                    this._tokenValue = response.Headers["Token"];
                }
                using (Stream stream = response.GetResponseStream())
                {
                    if (stream == null)
                    {
                        return(null);
                    }
                    using (reader = new StreamReader(stream, ri.Encoding))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                return(response);
            }
            catch (WebException exception)
            {
                response = exception.Response as HttpWebResponse;
                using (reader = new StreamReader(response.GetResponseStream(), ri.Encoding))
                {
                    result = reader.ReadToEnd();
                }
                return(response);
            }
        }
Esempio n. 8
0
        private HttpWebRequest CreatePostRequest(HttpRequestInfo ri)
        {
            HttpWebRequest request = WebRequest.Create(ri.Domain + "/" + ri.URI) as HttpWebRequest;

            if (ri.Domain.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(this.CheckValidationResult);
                request.ProtocolVersion = HttpVersion.Version10;
            }
            request.Method = "POST";
            this.SetRequest(request, ri);
            byte[] bytes = ri.Encoding.GetBytes(ri.Arguments);
            request.ContentLength = bytes.Length;
            request.Accept        = "application/json";
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);
            }
            return(request);
        }