System.Net.WebConnection.EnsureSSLStreamAvailable C# (CSharp) Method

EnsureSSLStreamAvailable() static private method

static private EnsureSSLStreamAvailable ( ) : void
return void
		static void EnsureSSLStreamAvailable ()
		{
			lock (classLock) {
				if (sslStream != null)
					return;

#if NET_2_1 && SECURITY_DEP
				sslStream = typeof (Mono.Security.Protocol.Tls.HttpsClientStream);
#else
				// HttpsClientStream is an internal glue class in Mono.Security.dll
				sslStream = Type.GetType ("Mono.Security.Protocol.Tls.HttpsClientStream, " +
							Consts.AssemblyMono_Security, false);

				if (sslStream == null) {
					string msg = "Missing Mono.Security.dll assembly. " +
							"Support for SSL/TLS is unavailable.";

					throw new NotSupportedException (msg);
				}
#endif
				piClient = sslStream.GetProperty ("SelectedClientCertificate");
				piServer = sslStream.GetProperty ("ServerCertificate");
				piTrustFailure = sslStream.GetProperty ("TrustFailure");
			}
		}

Usage Example

 private bool CreateStream(HttpWebRequest request)
 {
     try
     {
         System.Net.Sockets.NetworkStream networkStream = new System.Net.Sockets.NetworkStream(this.socket, false);
         if (request.Address.Scheme == System.Uri.UriSchemeHttps)
         {
             this.ssl = true;
             WebConnection.EnsureSSLStreamAvailable();
             if (!this.reused || this.nstream == null || this.nstream.GetType() != WebConnection.sslStream)
             {
                 byte[] array = null;
                 if (this.sPoint.UseConnect && !this.CreateTunnel(request, networkStream, out array))
                 {
                     return(false);
                 }
                 object[] args = new object[]
                 {
                     networkStream,
                     request.ClientCertificates,
                     request,
                     array
                 };
                 this.nstream = (Stream)Activator.CreateInstance(WebConnection.sslStream, args);
                 SslClientStream sslClientStream = (SslClientStream)this.nstream;
                 ServicePointManager.ChainValidationHelper @object = new ServicePointManager.ChainValidationHelper(request);
                 sslClientStream.ServerCertValidation2 += @object.ValidateChain;
                 this.certsAvailable = false;
             }
         }
         else
         {
             this.ssl     = false;
             this.nstream = networkStream;
         }
     }
     catch (Exception)
     {
         if (!request.Aborted)
         {
             this.status = WebExceptionStatus.ConnectFailure;
         }
         return(false);
     }
     return(true);
 }