/// <summary> /// Begins the get response. /// </summary> private void BeginGetResponse() { var proxyUri = Proxy.GetProxy(RequestUri); var credentials = (!string.IsNullOrEmpty(proxyUri?.AbsoluteUri) && !RequestUri.Equals((Proxy as WebProxy)?.Address)) ? (Proxy as WebProxySocks)?.Credentials as WebProxyCredential : null; _socksConnection = new SocketWithProxy(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, ProxyType.Socks5, credentials); _socksConnection.Connect(RequestUri.Host, RequestUri.Port); // Open the connection // Send request message _networkStream = new NetworkStream(_socksConnection); // Build request message if needed var requestMessage = BuildHttpRequestMessage(); var requestMessageData = Encoding.GetBytes(requestMessage); _networkStream.Write(requestMessageData, 0, requestMessageData.Length); }
protected override void Dispose(bool disposing) #endif { if (disposing) { Close(); try { _socksConnection?.Dispose(); _responseStream?.Dispose(); } finally { _socksConnection = null; _responseStream = null; } } #if !NET40 base.Dispose(disposing); #endif }
/// <summary> /// Initializes a new instance of the <see cref="HttpWebResponseSocks" /> class. /// </summary> /// <param name="responseUri">The response URI.</param> /// <param name="verb">The verb.</param> /// <param name="statusCode">The status code.</param> /// <param name="responseHeaders">The response headers.</param> /// <param name="responseStream">The response stream.</param> /// <exception cref="InvalidOperationException"></exception> internal HttpWebResponseSocks(Uri responseUri, KnownHttpVerb verb, HttpStatusCode statusCode, WebHeaderCollection responseHeaders, Stream responseStream) { StatusCode = statusCode; Headers = responseHeaders; ResponseUri = responseUri; _socksConnection = null; _verb = verb; // Build stream object for WebResponse string contentEncodingKey; DecompressionMethods compressionMethod; if (!Headers.ContainsHeaderKey(HttpResponseHeader.ContentEncoding.ToString(), out contentEncodingKey) || !Enum.TryParse(Headers[contentEncodingKey], true, out compressionMethod)) { _responseStream = responseStream; return; } // If content encoding is var contentEncoding = Headers[contentEncodingKey].Trim().ToLowerInvariant(); switch (compressionMethod) { case DecompressionMethods.GZip: _responseStream = new GZipStream(responseStream, CompressionMode.Decompress); Headers[HttpRequestHeader.ContentLength] = "-1"; break; case DecompressionMethods.Deflate: _responseStream = new DeflateStream(responseStream, CompressionMode.Decompress); Headers[HttpRequestHeader.ContentLength] = "-1"; break; default: throw new InvalidOperationException($"Unknown encoding type '{contentEncoding}'"); } }
/// <summary> /// Begins the get response. /// </summary> /// <param name="callback">The callback.</param> /// <param name="state">The state.</param> /// <returns></returns> /// <exception cref="InvalidOperationException"> /// Proxy property cannot be null. /// or /// Method has not been set. /// </exception> public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { if (Proxy == null) { throw new InvalidOperationException("Proxy property cannot be null."); } if (string.IsNullOrEmpty(Method)) { throw new InvalidOperationException("Method has not been set."); } var proxyUri = Proxy.GetProxy(RequestUri); var credentials = (!string.IsNullOrEmpty(proxyUri?.AbsoluteUri) && !RequestUri.Equals((Proxy as WebProxy)?.Address)) ? (Proxy as WebProxySocks)?.Credentials as WebProxyCredential : null; _socksConnection = new SocketWithProxy(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, ProxyType.Socks5, credentials); Callback = callback; // Open the connection return(_socksConnection.BeginConnect(RequestUri.Host, RequestUri.Port, EndConnectCallback, _socksConnection)); }