예제 #1
0
        /// <summary>
        /// 向服务器发送Request
        /// </summary>
        /// <param name="url">字符串</param>
        /// <param name="method">Get或Post</param>
        /// <param name="body">Post时必须传值</param>
        /// <returns></returns>
        public static byte[] Request(string url, MethodEnum method, string body = "")
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.UserAgent = DefaultUserAgent;
                request.Method    = method.ToString();
                if (method == MethodEnum.POST)//post设置body
                {
                    byte[] request_body = Encoding.UTF8.GetBytes(body);
                    request.ContentLength = request_body.Length;

                    Stream request_stream = request.GetRequestStream();
                    request_stream.Write(request_body, 0, request_body.Length);
                }
                if (CookiesContainer == null)
                {
                    CookiesContainer = new CookieContainer();
                }
                request.CookieContainer = CookiesContainer;

                return(Response(request));
            }
            catch {
                throw;
            }
        }
예제 #2
0
        /// <summary>
        /// 向服务器发送Request
        /// </summary>
        /// <param name="url">字符串</param>
        /// <param name="method">枚举类型的方法Get或者Post</param>
        /// <param name="body">Post时必须传值</param>
        /// <returns></returns>
        public static byte[] Request(string url, MethodEnum method, string body = "", string contentType = null)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = method.ToString();
                //如果是Post的话,则设置body
                if (method == MethodEnum.POST)
                {
                    byte[] request_body = Encoding.UTF8.GetBytes(body);
                    request.ContentLength = request_body.Length;
                    if (contentType != null)
                    {
                        request.ContentType = contentType;
                    }

                    Stream request_stream = request.GetRequestStream();
                    request_stream.Write(request_body, 0, request_body.Length);
                }
                if (CookiesContainer == null)
                {
                    CookiesContainer = new CookieContainer();
                }
                //启用Cookie
                request.CookieContainer = CookiesContainer;

                return(Response(request));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
예제 #3
0
        private Byte[] doHttpRequest(string URL)
        {
            string          downloadedcontent = string.Empty;
            HttpWebResponse _httpResponse     = null;
            HttpWebRequest  _httprequest      = null;

            try
            {
                _httprequest = (HttpWebRequest)HttpWebRequest.Create(this.Url);

                //logger_Debug("Request stream parameters setting");
                //set request parameters before calling HTTP request
                if (_requestParams != null)
                {
                    CookieContainer cc = new CookieContainer();
                    cc.Add(_requestParams.Cookies);
                    _httprequest.CookieContainer = cc;
                    foreach (string key in _requestParams.Headers.AllKeys)
                    {
                        _httprequest.Headers.Add(key, _requestParams.Headers[key]);
                    }
                    //_httprequest.Headers = _requestParams.Headers;
                }
                _httprequest.UserAgent   = Helper.GetUserAgent(this.UserAgent);
                _httprequest.Timeout     = this.Timeout;
                _httprequest.Proxy       = this.Proxy ?? WebRequest.DefaultWebProxy;
                _httprequest.Method      = _method.ToString();
                _httprequest.Credentials = _credentials;
                if (!string.IsNullOrEmpty(this.Referer))
                {
                    _httprequest.Referer = this.Referer;
                }

                if (!string.IsNullOrEmpty(_requestParams?.Accept))
                {
                    _httprequest.Accept = _requestParams.Accept;
                }
                if (!string.IsNullOrEmpty(_requestParams?.Referer))
                {
                    _httprequest.Referer = _requestParams.Referer;
                }

                if (_requestParams != null)
                {
                    _httprequest.AllowAutoRedirect = _requestParams.AllowAutoRedirect;
                }

                if (_credentials != null && !string.IsNullOrEmpty(_credentials.UserName) && !string.IsNullOrEmpty(_credentials.Password))
                {
                    string ticket = string.Format("{0}:{1}", _credentials.UserName, _credentials.Password);
                    _httprequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(ticket)));
                }

                _httprequest.ContentType = this.ContentType;

                //if POST, add post data into request stream
                if (_method == MethodEnum.POST)
                {
                    Stream streamReq = _httprequest.GetRequestStream();
                    string postData  = this.RequestParams.FormInPostDataFormat();
                    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                    streamReq.Write(byteArray, 0, byteArray.Length);
                    streamReq.Close();
                }


                //logger_Debug("Request started, loading data");

                //Open request, load data
                try
                {
                    _httpResponse = (HttpWebResponse)_httprequest.GetResponse();
                }
                catch (WebException e_h)
                {
                    _httpResponse = (HttpWebResponse)e_h.Response;
                    if (_httpResponse.StatusCode != HttpStatusCode.Redirect)
                    {
                        throw (e_h);
                    }
                }

                _responseParams  = new ParametersContainer(_httpResponse.Headers, _httpResponse.Cookies);
                this.ContentType = _httpResponse.ContentType;

                byte[] rawdata = ReadAllBytesFromHttpStream(_httpResponse.GetResponseStream());
                this.ResponseUrl = _httpResponse.ResponseUri?.AbsoluteUri ?? this.Url;
                return(rawdata);
            }
            catch (WebException ex)
            {
                if (this.Proxy != null)
                {
                    this.Proxy.SetStatus(new TestProxyResult()
                    {
                        Error = ex, Success = false
                    });
                }

                byte[] rawdata = null;
                if (ex.Response != null)
                {
                    this.ResponseUrl = ex.Response.ResponseUri?.AbsoluteUri ?? this.Url;
                    MemoryStream exmemStream = new MemoryStream();
                    if (ex.Response.GetResponseStream() != null && ex.Response.GetResponseStream().CanRead)
                    {
                        rawdata = ReadAllBytesFromHttpStream(ex.Response.GetResponseStream());
                        //var encoding = GetCharset(ref rawdata);
                        //string exdownloadedcontent = encoding.GetString(rawdata);
                    }
                }

                logger_Error(this.ToString() + "\n\n", ex);
                //throw new WebException(exdownloadedcontent, ex);
                throw new UrlContentException(this.ToString(), ex)
                      {
                          DownloadedContent = rawdata
                      };
            }
            catch (Exception exc)
            {
                if (this.Proxy != null)
                {
                    this.Proxy.SetStatus(new TestProxyResult()
                    {
                        Error = exc, Success = false
                    });
                }

                logger_Error(this.ToString(), exc);
                throw new UrlContentException("", exc);
            }
            finally
            {
                _httprequest = null;
                if (!(_httpResponse == null))
                {
                    _httpResponse.Close();
                }
                //logger_Debug("Request stream closed");
                if (_method == MethodEnum.POST)
                {
                    //clear special config for POSTing data
                    DisablePostConfig();
                }
            }
        }