Example #1
0
        public static void SetPoolInfo(Uri host, int maxConn, int timeout, bool async = true)
        {
            HttpClientPool pool = GetPool(null, host);

            pool.MaxConnections = maxConn;
            pool.TimeOut        = timeout;
            pool.Async          = async;
        }
Example #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);
                }
            }
        }
Example #3
0
 private static HttpClientPool CreatePool(string key, Uri uri)
 {
     lock (typeof(HttpClientPoolFactory))
     {
         HttpClientPool result;
         if (!mPools.TryGetValue(key, out result))
         {
             result      = new HttpClientPool(uri.Host, uri.Port);
             mPools[key] = result;
         }
         return(result);
     }
 }
Example #4
0
 private static HttpClientPool CreatePool(string key, Uri uri)
 {
     lock (typeof(HttpClientPoolFactory))
     {
         HttpClientPool result;
         if (!mPools.TryGetValue(key, out result))
         {
             bool ssl = uri.Scheme.ToLower() == "https";
             result      = new HttpClientPool(uri.Host, uri.Port, ssl);
             mPools[key] = result;
         }
         return(result);
     }
 }