예제 #1
0
 public HttpHost(Uri host)
 {
     this.Uri  = host;
     Formater  = new FormUrlFormater();
     Host      = this.Uri.Host;
     Port      = this.Uri.Port;
     mPoolKey  = $"{this.Uri.Host}:{this.Uri.Port}";
     mPool     = HttpClientPoolFactory.GetPool(mPoolKey, this.Uri);
     Available = true;
     InVerify  = false;
 }
예제 #2
0
        public Response Execute(Request request, Type bodyType = null)
        {
            HttpClientPool pool      = HttpClientPoolFactory.GetPool(mPoolKey, this.Uri);
            TcpClient      tcpClient = pool.Pop();

            try
            {
                tcpClient.SendMessage(request);
                var result = tcpClient.ReadMessage <Response>();
                ResponseHeader = result.Header;
                int code = int.Parse(result.Code);
                if (result.Length > 0)
                {
                    try
                    {
                        if (code < 400)
                        {
                            result.Body = request.Formater.Deserialization(result.Stream, bodyType, result.Length);
                        }
                        else
                        {
                            result.Body = result.Stream.ReadString(result.Length);
                        }
                    }
                    finally
                    {
                        result.Stream.ReadFree(result.Length);
                        if (result.Chunked)
                        {
                            result.Stream.Dispose();
                        }
                        result.Stream = null;
                    }
                }
                if (!result.KeepAlive)
                {
                    tcpClient.DisConnect();
                }
                if (code >= 400)
                {
                    throw new System.Net.WebException($"{request.Method} {request.Url} {result.Code} {result.CodeMsg} {result.Body}");
                }
                return(result);
            }
            finally
            {
                if (tcpClient != null)
                {
                    pool.Push(tcpClient);
                }
            }
        }