Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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));
        }