Пример #1
0
        Connect()
        {
            Uri uri = CurrentRequest.HasProxy ? CurrentRequest.Proxy.Address : CurrentRequest.CurrentUri;

            if (Client == null)
            {
                Client = new TcpClient();
            }

            if (!Client.Connected)
            {
                Client.ConnectTimeout = CurrentRequest.ConnectTimeout;

//#if NETFX_CORE
                //await
//#endif
                string address = HTTPManager.GetHostCache(uri.Host);
                if (!string.IsNullOrEmpty(address))
                {
                    Client.Connect(address, uri.Port);
                }
                else
                {
                    Client.Connect(uri.Host, uri.Port);
                    if (Client.IsConnected())
                    {
                        System.Net.IPEndPoint end_point = Client.Client.RemoteEndPoint as System.Net.IPEndPoint;
                        address = end_point.Address.ToString();
                        HTTPManager.AddHostCache(uri.Host, address);
                    }
                }
            }
            else
            {
//				NGUIDebug.print("Client.Connected ========>" + Client.Connected);
            }

            if (Stream == null)
            {
                if (HasProxy && !Proxy.IsTransparent)
                {
                    Stream = Client.GetStream();

                    var outStream = new BinaryWriter(Stream);
                    outStream.Write(string.Format("CONNECT {0}:{1} HTTP/1.1", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port).GetASCIIBytes());
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Write(string.Format("Host: {0}:{1}", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port).GetASCIIBytes());
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Write(string.Format("Proxy-Connection: Keep-Alive"));
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Flush();

                    ReadTo(Stream, HTTPResponse.LF);
                    ReadTo(Stream, HTTPResponse.LF);
                }

                if (HTTPProtocolFactory.IsSecureProtocol(uri))
                {
                    // On WP8 there are no Mono, so we must use the 'alternate' TlsHandlers
#if !UNITY_WP8 && !NETFX_CORE
                    if (CurrentRequest.UseAlternateSSL)
                    {
#endif
                    var handler = new TlsClientProtocol(Client.GetStream(), new Org.BouncyCastle.Security.SecureRandom());
                    handler.Connect(new LegacyTlsClient(new AlwaysValidVerifyer()));
                    Stream = handler.Stream;
#if !UNITY_WP8 && !NETFX_CORE
                }
                else
                {
                    SslStream sslStream = new SslStream(Client.GetStream(), false, (sender, cert, chain, errors) =>
                    {
                        return(CurrentRequest.CallCustomCertificationValidator(cert, chain));
                    });

                    if (!sslStream.IsAuthenticated)
                    {
                        sslStream.AuthenticateAsClient(uri.Host);
                    }
                    Stream = sslStream;
                }
#endif
                }
                else
                {
                    Stream = Client.GetStream();
                }
            }
        }