Exemplo n.º 1
0
        public IHttpResponse GetResponse()
        {
            SocketHttpResponse response = new SocketHttpResponse(Url);
            int retry = _nbRetry;

            do
            {
                try
                {
                    ProcessUri(Url, response);
                    return(response);
                }
                catch (Exception e)
                {
                    retry--;
                    if (retry == 0)
                    {
                        response.StatusCode = 0;
                        response.Headers["ErrorMessage"] = e.Message;
                        response.Headers["StackTrace"]   = e.StackTrace;
                    }
                }
            } while (retry > 0);

            return(response);
        }
Exemplo n.º 2
0
        public IHttpResponse GetResponse(SocketProxy socket)
        {
            SocketHttpResponse response = new SocketHttpResponse(Url);

            ProcessUri(Url, response, socket.Stream);

            return(response);
        }
Exemplo n.º 3
0
        private void ProcessUri(Uri uri, SocketHttpResponse response)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();
            using (SocketReference socketReference = _socketFactory.GetSocket(uri))
            {
                watch.Stop();
                response.Timing.OpeningConnection = watch.Elapsed;
                ProcessUri(uri, response, socketReference.Socket.Stream);
                if (response.Headers.TryGetValue("Connection") == "close")
                {
                    socketReference.Socket.Socket.Close();
                }
            }
        }
Exemplo n.º 4
0
        private void ProcessUri(Uri uri, SocketHttpResponse response, Stream stream)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();
            SocketWriter writer = new SocketWriter(stream);

            writer.WriteLine(Method + " " + uri.PathAndQuery + " HTTP/1.1");
            foreach (KeyValuePair <string, string> requestHeader in Headers)
            {
                writer.WriteLine(requestHeader.Key + ": " + requestHeader.Value);
            }

            writer.WriteLine("");
            if (Body != null)
            {
                writer.WriteLine(Body);
            }
            stream.Flush();
            //sb.AppendLine("Connection: keep-alive");

            response.Timing.RequestSent = watch.Elapsed;
            watch.Restart();

            string status = writer.ReadLine();

            response.StatusCode = (HttpStatusCode)Int32.Parse(status.Split(' ')[1]);

            response.Timing.Waiting = watch.Elapsed;
            watch.Restart();

            string responseHeader;

            while (!String.IsNullOrEmpty(responseHeader = writer.ReadLine()))
            {
                string[] parts = responseHeader.Split(new char[] { ':' }, 2);
                if (parts.Length > 0)
                {
                    response.Headers[parts[0].Trim()] = parts[1].Trim();
                }
            }

            string strContentLength = response.Headers.TryGetValue("Content-Length");
            string transferEncoding = response.Headers.TryGetValue("Transfer-Encoding");

            if (transferEncoding == "chunked")
            {
                response.CompressedContent = writer.ReadByChunks();
            }
            else if (response.StatusCode == HttpStatusCode.NotModified)
            {
                response.CompressedContent = writer.Read(0);
            }
            else if (String.IsNullOrEmpty(strContentLength))
            {
                response.CompressedContent = writer.ReadToEnd();
            }
            else
            {
                response.CompressedContent = writer.Read(Int32.Parse(strContentLength));
            }

            switch (response.Headers.TryGetValue("Content-Encoding"))
            {
            case "gzip":
                response.Content = response.CompressedContent.DecodeGZip();
                break;

            default:
                response.Content = response.CompressedContent;
                break;
            }

            response.Timing.Download = watch.Elapsed;
        }