MSDN Library

ProtectedWebConnection Class

Namespace: System.Net.Security

Represents a protected Web connection using Secure Sockets Layer (SSL) or Transport Layer Security (TLS). This class allows for the establishment and management of secure HTTP connections, enabling confidentiality and integrity of data exchanged between a client and a server.

Syntax


public class ProtectedWebConnection : WebConnection
{
    // Fields
    // public static readonly TimeSpan DefaultKeepAliveTimeout;

    // Constructors
    public ProtectedWebConnection(Uri url, HttpRequest request, HttpResponse response, HttpClientHandler handler);
    public ProtectedWebConnection(Uri url, HttpRequest request, HttpResponse response, HttpClientHandler handler, bool useProxy);

    // Methods
    // public override void Abort();
    // public override void Close();
    // public override void Send(HttpRequest request);
    // public override void Send(HttpRequest request, HttpWebResponse response);
    // protected override void Dispose(bool disposing);
    // public override Stream GetRequestStream();
    // public override Stream GetResponseStream();
    // public override void SetRequestHeaders(HttpRequest request);
}
            

Remarks

The ProtectedWebConnection class is an internal implementation detail used by HttpClientHandler to manage secure connections. It is not intended to be used directly by application developers. It handles the SSL/TLS handshake, certificate validation, and the underlying secure stream communication.

When you make an HTTPS request using HttpClient with a handler that supports secure connections, HttpClientHandler may instantiate and use ProtectedWebConnection to manage the secure communication channel.

Key Features:

Inheritance Hierarchy

object
WebConnection
ProtectedWebConnection

Example Usage (Conceptual)

While you won't directly instantiate ProtectedWebConnection, understanding its role helps in debugging and configuring secure HTTP communication.


using System;
using System.Net.Http;
using System.Net.Security;
using System.Threading.Tasks;

// This is a conceptual example. You typically don't interact with ProtectedWebConnection directly.

public class SecureHttpClientExample
{
    public static async Task MakeSecureRequestAsync()
    {
        // HttpClientHandler is responsible for managing connections, including secure ones.
        // When making an HTTPS request, it might use ProtectedWebConnection internally.
        using (var httpClient = new HttpClient())
        {
            try
            {
                // The handler will automatically use SSL/TLS for HTTPS URLs.
                var response = await httpClient.GetAsync("https://www.example.com");
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request error: {e.Message}");
            }
        }
    }
}
            

Requirements

The ProtectedWebConnection class is part of the System.Net.Security namespace and is available in the .NET Framework and .NET Core/.NET 5+.

Assembly

System.dll

Namespace

System.Net.Security

Platform

Windows, Linux, macOS