Пример #1
0
        public void TestToStringWithEncodingReadFromHeader()
        {
            var encoding = Encoding.GetEncoding(936);

            var response = new HttpResponse
            {
                Bytes = new byte[]
                                               {
                                                   0x30,
                                                   0x00,
                                                   0xA6,
                                                   0xE2,
                                                   0xB2,
                                                   0xDF,
                                                   0xA6,
                                                   0xF5
                                               },
                Headers = new Dictionary<string, string>
                          {
                              {"Content-Type", "charset=" + encoding.HeaderName}
                          }
            };

            Assert.AreEqual("0\0︹策︴", response.ToString());
        }
Пример #2
0
        public void TestConstructor()
        {
            var response = new HttpResponse();

            Assert.IsNotNull(response.Cookies);
            Assert.AreEqual(0, response.Headers.Count);
        }
Пример #3
0
        public void TestToStringWithEncoding()
        {
            var response = new HttpResponse
                               {
                                   Bytes = new byte[]
                                               {
                                                   0x30,
                                                   0x00,
                                                   0xA6,
                                                   0xE2,
                                                   0xB2,
                                                   0xDF,
                                                   0xA6,
                                                   0xF5
                                               }
                               };

            Assert.AreEqual("0\0︹策︴", response.ToString(Encoding.GetEncoding(936)));
        }
Пример #4
0
        /// <summary>
        /// Maps the specified <see cref="WebResponse" /> to the given <see cref="HttpResponse" />.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="webResponse">The web response.</param>
        /// <param name="response">The HTTP response to map the <see cref="webResponse" />to.</param>
        public static void Map(HttpRequest request, WebResponse webResponse, ref HttpResponse response)
        {
            if (webResponse != null)
            {
                // If the web response followed an http redirect, the URL will have changed. Reflect that change.
                response.RedirectedUrl = webResponse.ResponseUri.ToString();

                response.ContentLength = webResponse.ContentLength;

                foreach (string header in webResponse.Headers)
                {
                    response.Headers.Add(header, webResponse.Headers[header]);
                }

                var httpWebResponse = webResponse as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    response.StatusCode = httpWebResponse.StatusCode;
                    response.StatusDescription = httpWebResponse.StatusDescription;
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Downloads the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public HttpResponse Download(HttpRequest request)
        {
            HttpResponse response;

            try
            {
                response = Retry.This(() => InternalDownload(request), request.Retries, request.Timeout);
            }
            catch (WebException ex)
            {
                response = new HttpResponse
                               {
                                   Exception = ex,
                                   Url = request.Url
                               };

                Map(request, ex.Response, ref response);
            }
            catch (Exception ex)
            {
                response = new HttpResponse
                               {
                                   Exception = ex,
                                   Url = request.Url
                               };
            }

            return response;
        }
Пример #6
0
        /// <summary>
        /// Downloads the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        protected virtual HttpResponse InternalDownload(HttpRequest request)
        {
            var result = new HttpResponse
                         {
                             Url = request.Url
                         };

            var webRequest = InternalBuild(request);

            // After the poodle security exploit in SSLv3 most servers are no longer supporting the SSLv3 cypher
            ServicePointManager.SecurityProtocol = request.SecurityProtocol;

            // Post request data
            if (request.Verb == HttpVerb.Post && request.Data != null)
            {
                using (var stream = webRequest.GetRequestStream())
                {
                    var encoder = new UTF8Encoding();

                    var data = encoder.GetBytes(request.Data);

                    stream.Write(data, 0, data.Length);

                    stream.Close();
                }
            }

            // Download response
            using (var webResponse = webRequest.GetResponse() as HttpWebResponse)
            {
                if (request.Verb != HttpVerb.Head)
                {
                    using (var stream = webResponse.GetResponseStream())
                    using (var memoryStream = new MemoryStream())
                    using (var reader = new BinaryReader(stream, request.Encoding))
                    using (var writer = new BinaryWriter(memoryStream, request.Encoding))
                    {
                        var buffer = new byte[2048];

                        int bytesRead;

                        do
                        {
                            bytesRead = reader.Read(buffer, 0, buffer.Length);

                            writer.Write(buffer, 0, bytesRead);
                        }
                        while (bytesRead != 0);

                        if (memoryStream.Length > 0) result.Bytes = memoryStream.ToArray();

                        result.Cookies = new CookieContainer();
                        result.Cookies.Add(webResponse.Cookies);
                    }
                }

                Map(request, webResponse, ref result);
            }

            return result;
        }
Пример #7
0
        public void TestToStringOnEmptyRepsonse()
        {
            var response = new HttpResponse();

            Assert.AreEqual(string.Empty, response.ToString());
        }