public static Stream ConnectStream(Uri uri, IWebProxy proxy, bool nodelay, int timeout_ms)
{
IMockWebProxy mockProxy = proxy != null ? proxy as IMockWebProxy : null;
if (mockProxy != null)
return mockProxy.GetStream(uri);
Stream stream;
bool useProxy = proxy != null && !proxy.IsBypassed(uri);
if (useProxy)
{
Uri proxyURI = proxy.GetProxy(uri);
stream = ConnectSocket(proxyURI, nodelay, timeout_ms);
}
else
{
stream = ConnectSocket(uri, nodelay, timeout_ms);
}
try
{
if (useProxy)
{
string line = String.Format("CONNECT {0}:{1} HTTP/1.0", uri.Host, uri.Port);
WriteLine(line, stream);
WriteLine(stream);
ReadHttpHeaders(ref stream, proxy, nodelay, timeout_ms);
}
if (UseSSL(uri))
{
SslStream sslStream = new SslStream(stream, false,
new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
sslStream.AuthenticateAsClient("", null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, true);
stream = sslStream;
}
return stream;
}
catch
{
stream.Close();
throw;
}
}