示例#1
0
        /// <summary>
        /// Create the response from the request
        /// </summary>
        /// <returns></returns>
        public static SynchronousWebResponse Create(WebRequest request, bool asStream)
        {
            var webResponse = new SynchronousWebResponse();

            Stopwatch watch = Stopwatch.StartNew();

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                watch.Stop();

                webResponse.ElapsedMilliseconds = watch.ElapsedMilliseconds;
                webResponse.ContentEncoding     = response.ContentEncoding;
                webResponse.ContentLength       = response.ContentLength;
                webResponse.ContentType         = response.ContentType;
                webResponse.Headers             = response.Headers;
                webResponse.Status  = response.StatusCode;
                webResponse.Cookies = response.Cookies;

                using (Stream responseStream = response.GetResponseStream())
                {
                    if (responseStream == null)
                    {
                        return(webResponse);
                    }

                    if (!asStream)
                    {
                        using (var readStream = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            webResponse.Body = readStream.ReadToEnd();
                        }
                    }
                    else
                    {
                        webResponse.BodyStream = new MemoryStream();
                        responseStream.CopyTo(webResponse.BodyStream);
                        webResponse.BodyStream.Seek(0, SeekOrigin.Begin);
                    }
                }
            }

            return(webResponse);
        }
        public SynchronousWebResponse DoGetResponse(bool asStream)
        {
            string requestData = EncodeRequestParams();

            HttpWebRequest request;

            if (_httpMethod == HttpConstants.HttpMethods.Post)
            {
                var uri = new Uri(_requestUri);
                request             = (HttpWebRequest)WebRequest.Create(uri);
                request.Referer     = _referer;
                request.Method      = HttpConstants.HttpMethods.Post;
                request.ContentType = _contentType.IsNullOrEmpty() ? HttpConstants.ContentTypes.FormUrlEncoded : _contentType;
                request.Timeout     = _timeout;
                request.UserAgent   = _userAgent;
                request.Accept      = _accept;
                request.Host        = _host;
                request.Connection  = _connection;

                //get the request data byte array
                byte[] bytes = Encoding.UTF8.GetBytes(requestData);

                //set the content length
                request.ContentLength = bytes.Length;

                //write to the request stream
                using (var requestStream = request.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                }
            }
            else
            {
                var uri = new Uri(string.Format("{0}{1}{2}",
                                                _requestUri,
                                                _requestUri.Contains("?") ? "&" : "?",
                                                requestData));

                request           = (HttpWebRequest)WebRequest.Create(uri);
                request.Method    = _httpMethod;
                request.Referer   = _referer;
                request.Timeout   = _timeout;
                request.UserAgent = _userAgent;
            }

            //set the cookies to the request
            if (_requestCookies.Count > 0)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(_requestCookies);
            }

            //assign any headers to the request
            if (_requestHeaders.Count > 0)
            {
                request.Headers.Add(_requestHeaders);
            }

            return(SynchronousWebResponse.Create(request, asStream));
        }