Пример #1
0
        private async void OnExecute(AnyCompletionSource <Response> taskCompletionSource)
        {
            HttpClient client = null;

            try
            {
                client = HttpHost.Pool.Pop();
                client.RequestCommpletionSource = taskCompletionSource;
                Client = client.Client;
                if (client.Client is AsyncTcpClient)
                {
                    AsyncTcpClient asyncClient = (AsyncTcpClient)client.Client;
                    var            a           = asyncClient.ReceiveMessage <Response>();
                    if (!a.IsCompleted)
                    {
                        asyncClient.Send(this);
                        Status = RequestStatus.SendCompleted;
                    }
                    Response = await a;
                    if (Response.Exception != null)
                    {
                        Status = RequestStatus.Error;
                        throw Response.Exception;
                    }
                    else
                    {
                        Status = RequestStatus.Received;
                    }
                }
                else
                {
                    TcpClient syncClient = (TcpClient)client.Client;
                    syncClient.SendMessage(this);
                    Status   = RequestStatus.SendCompleted;
                    Response = syncClient.ReceiveMessage <Response>();
                    if (Response.Exception != null)
                    {
                        Status = RequestStatus.Error;
                        throw Response.Exception;
                    }
                    else
                    {
                        Status = RequestStatus.Received;
                    }
                }
                int code = int.Parse(Response.Code);
                if (Response.Length > 0)
                {
                    try
                    {
                        if (code < 400)
                        {
                            Response.Body = this.Formater.Deserialization(Response.Stream, this.BodyType, Response.Length);
                        }
                        else
                        {
                            Response.Body = Response.Stream.ReadString(Response.Length);
                        }
                    }
                    finally
                    {
                        Response.Stream.ReadFree(Response.Length);
                        if (Response.Chunked)
                        {
                            Response.Stream.Dispose();
                        }
                        Response.Stream = null;
                    }
                }
                if (!Response.KeepAlive)
                {
                    client.Client.DisConnect();
                }
                if (code >= 400)
                {
                    throw new System.Net.WebException($"{this.Method} {this.Url} {Response.Code} {Response.CodeMsg} {Response.Body}");
                }
                Status = RequestStatus.Completed;
            }
            catch (Exception e_)
            {
                HttpClientException clientException = new HttpClientException(this, HttpHost.Uri, e_.Message, e_);
                if (e_ is System.Net.Sockets.SocketException || e_ is ObjectDisposedException)
                {
                    clientException.SocketError = true;
                }
                Response = new Response {
                    Exception = clientException
                };
                Status = RequestStatus.Error;
            }
            if (Response.Exception != null)
            {
                HttpHost.AddError(Response.Exception.SocketError);
            }
            else
            {
                HttpHost.AddSuccess();
            }
            Response.Current = Response;
            if (client != null)
            {
                HttpHost.Pool.Push(client);
            }
            await Task.Run(() => taskCompletionSource.TrySetResult(Response));
        }
Пример #2
0
        private async void OnExecute(AnyCompletionSource <Response> taskCompletionSource)
        {
            HttpClient client = null;
            Response   response;

            try
            {
                client = HttpHost.Pool.Pop();
                client.RequestCommpletionSource = taskCompletionSource;
                Client = client.Client;
                if (client.Client is AsyncTcpClient)
                {
                    AsyncTcpClient asyncClient = (AsyncTcpClient)client.Client;
                    var            a           = asyncClient.ReceiveMessage();
                    if (!a.IsCompleted)
                    {
                        asyncClient.Send(this);
                        Status = RequestStatus.SendCompleted;
                    }
                    var result = await a;
                    if (result is Exception error)
                    {
                        response           = new Response();
                        response.Exception = new HttpClientException(this, HttpHost.Uri, error.Message, error);
                        Status             = RequestStatus.Error;
                    }
                    else
                    {
                        response = (Response)result;
                        Status   = RequestStatus.Received;
                    }
                }
                else
                {
                    TcpClient syncClient = (TcpClient)client.Client;
                    syncClient.SendMessage(this);
                    Status   = RequestStatus.SendCompleted;
                    response = syncClient.ReceiveMessage <Response>();
                    Status   = RequestStatus.Received;
                }
                if (response.Exception == null)
                {
                    int code = int.Parse(response.Code);
                    if (response.Length > 0)
                    {
                        try
                        {
                            if (code < 400)
                            {
                                response.Body = this.Formater.Deserialization(response.Stream, this.BodyType, response.Length);
                            }
                            else
                            {
                                response.Body = response.Stream.ReadString(response.Length);
                            }
                        }
                        finally
                        {
                            response.Stream.ReadFree(response.Length);
                            if (response.Chunked)
                            {
                                response.Stream.Dispose();
                            }
                            response.Stream = null;
                        }
                    }
                    if (!response.KeepAlive)
                    {
                        client.Client.DisConnect();
                    }
                    if (code >= 400)
                    {
                        response.Exception = new HttpClientException(this, HttpHost.Uri, $" [{this.Method} {this.Url} {response.Code} {response.CodeMsg}] Response text:[{response.Body}]");
                    }
                    Status = RequestStatus.Completed;
                }
            }
            catch (Exception e_)
            {
                HttpClientException clientException = new HttpClientException(this, HttpHost.Uri, e_.Message, e_);
                response = new Response {
                    Exception = clientException
                };
                Status = RequestStatus.Error;
            }
            if (response.Exception != null)
            {
                HttpHost.AddError(response.Exception.SocketError);
            }
            else
            {
                HttpHost.AddSuccess();
            }
            Response.Current = response;
            this.Response    = response;
            if (client != null)
            {
                HttpHost.Pool.Push(client);
            }
            await Task.Run(() => taskCompletionSource.TrySetResult(response));
        }