Exemplo n.º 1
0
        private async Task <HttpResponseMessage> ReadHttpResponseMessage(SocksHttpManagerParameters parameters, Uri uri, Func <NetworkStream, Uri, CancellationToken, Task> sendRequest)
        {
            using (TcpClientExtended tcpClient = new TcpClientExtended())
            {
                Uri proxyUri = GetProxyUri(parameters, uri);

                await tcpClient.ConnectAsync(proxyUri.Host, proxyUri.Port, parameters.CancellationToken);

                await sendRequest(tcpClient.GetStream(), uri, parameters.CancellationToken);

                StringBuilder responseBuilder = new StringBuilder();

                byte[] requestBytes = Encoding.UTF8.GetBytes(BuildHttpRequestMessage(parameters.Request));
                await tcpClient.GetStream().WriteAsync(requestBytes, 0, requestBytes.Length, parameters.CancellationToken);

                FireEventProgress(parameters.ReportRequestProgress, requestBytes.Length, requestBytes.Length);

                long?total         = null;
                var  buffer        = new byte[1024];
                var  bytesReceived = await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length, parameters.CancellationToken);

                while (bytesReceived > 0)
                {
                    responseBuilder.Append(Encoding.UTF8.GetString(buffer, 0, bytesReceived));

                    if (parameters.ReportResponseProgress != null && !total.HasValue)
                    {
                        total = GetContentLength(responseBuilder.ToString());
                    }

                    if (total.HasValue)
                    {
                        FireEventProgress(parameters.ReportResponseProgress, responseBuilder.Length, total);
                    }

                    bytesReceived = await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length, parameters.CancellationToken);
                }

                HttpResponseMessage result = BuildHttpResponseMessage(responseBuilder.ToString());

                FireEventProgress(parameters.ReportResponseProgress, responseBuilder.Length, responseBuilder.Length);
                return(result);
            }
        }
Exemplo n.º 2
0
        private async Task<HttpResponseMessage> ReadHttpResponseMessage(SocksHttpManagerParameters parameters, Uri uri, Func<NetworkStream, Uri, CancellationToken, Task> sendRequest)
        {
            using (TcpClientExtended tcpClient = new TcpClientExtended())
            {
                Uri proxyUri = GetProxyUri(parameters, uri);

                await tcpClient.ConnectAsync(proxyUri.Host, proxyUri.Port, parameters.CancellationToken);

                await sendRequest(tcpClient.GetStream(), uri, parameters.CancellationToken);

                StringBuilder responseBuilder = new StringBuilder();

                byte[] requestBytes = Encoding.UTF8.GetBytes(BuildHttpRequestMessage(parameters.Request));
                await tcpClient.GetStream().WriteAsync(requestBytes, 0, requestBytes.Length, parameters.CancellationToken);
                FireEventProgress(parameters.ReportRequestProgress, requestBytes.Length, requestBytes.Length);

                long? total = null;
                var buffer = new byte[1024];
                var bytesReceived = await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length, parameters.CancellationToken);

                while (bytesReceived > 0)
                {
                    responseBuilder.Append(Encoding.UTF8.GetString(buffer, 0, bytesReceived));

                    if (parameters.ReportResponseProgress != null && !total.HasValue)
                    {
                        total = GetContentLength(responseBuilder.ToString());
                    }

                    if (total.HasValue)
                    {
                        FireEventProgress(parameters.ReportResponseProgress, responseBuilder.Length, total);
                    }

                    bytesReceived = await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length, parameters.CancellationToken);
                }

                HttpResponseMessage result = BuildHttpResponseMessage(responseBuilder.ToString());

                FireEventProgress(parameters.ReportResponseProgress, responseBuilder.Length, responseBuilder.Length);
                return result;
            }
        }
Exemplo n.º 3
0
        protected override async Task <bool> Alive(Proxy proxy, TaskItem task, Action begin, Action <int> firstTime, Action <int> end, CancellationTokenSource cancellationToken)
        {
            task.UpdateDetails(string.Format(Resources.OpeningConnectionFormat, proxy));

            try
            {
                using (TcpClientExtended tcpClient = new TcpClientExtended())
                {
                    await tcpClient.ConnectAsync(proxy.Address, proxy.Port, cancellationToken.Token);
                }
            }
            catch (TaskCanceledException)
            {
                return(false);
            }
            catch (SocketException)
            {
                return(false);
            }

            return(true);
        }