From 9aac5eace03b95f52f66ef6b1d4e8130b221f4be Mon Sep 17 00:00:00 2001 From: fcdeveloper00 Date: Mon, 22 Sep 2025 11:21:03 -0700 Subject: [PATCH 1/4] Update httpclient-migrate-from-httpwebrequest.md some text improvements. --- .../httpclient-migrate-from-httpwebrequest.md | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md b/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md index 036ac300cf0aa..515ac1d826a5d 100644 --- a/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md +++ b/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md @@ -66,7 +66,7 @@ HttpClient client = new(); using HttpResponseMessage responseMessage = await client.PostAsync(uri, new StringContent("Hello World!")); ``` -## HttpWebRequest to HttpClient, SocketsHttpHandler migration guide +## `HttpWebRequest` to `HttpClient`, `SocketsHttpHandler` migration guide | Old API | New API | Notes | |------------------------------------------|---------|-------| @@ -120,9 +120,9 @@ using HttpResponseMessage responseMessage = await client.PostAsync(uri, new Stri | `UseDefaultCredentials` | No direct equivalent API | [Example: Setting SocketsHttpHandler Properties](#example-set-socketshttphandler-properties). | | `UserAgent` | | [Example: Set Request Headers](#example-set-common-request-headers). | -## Migrate ServicePoint(Manager) usage +## Migrate `ServicePoint`(`Manager`) usage -You should be aware that `ServicePointManager` is a static class, meaning that any changes made to its properties will have a global effect on all newly created `ServicePoint` objects within the application. For example, when you modify a property like `ConnectionLimit` or `Expect100Continue`, it impacts every new ServicePoint instance. +You should be aware that `ServicePointManager` is a static class, meaning that any changes made to its properties will have a global effect on all newly created `ServicePoint` objects within the application. For example, when you modify a property like `ConnectionLimit` or `Expect100Continue`, it impacts every new `ServicePoint` instance. > [!WARNING] > In modern .NET, `HttpClient` does not take into account any configurations set on `ServicePointManager`. @@ -181,19 +181,19 @@ You should be aware that `ServicePointManager` is a static class, meaning that a | `CloseConnectionGroup` | No equivalent | No workaround | | `SetTcpKeepAlive` | No direct equivalent API | [Usage of SocketsHttpHandler and ConnectCallback](#usage-of-socketshttphandler-and-connectcallback). | -## Usage of HttpClient and HttpRequestMessage properties +## Usage of `HttpClient` and `HttpRequestMessage` properties -When working with HttpClient in .NET, you have access to a variety of properties that allow you to configure and customize HTTP requests and responses. Understanding these properties can help you make the most of HttpClient and ensure that your application communicates efficiently and securely with web services. +When working with HttpClient in .NET, you have access to a variety of properties that allow you to configure and customize HTTP requests and responses. Understanding these properties can help you make the most of `HttpClient` and ensure that your application communicates efficiently and securely with web services. -### Example: Usage of HttpRequestMessage properties +### Example: Usage of `HttpRequestMessage` properties -Here's an example of how to use HttpClient and HttpRequestMessage together: +Here's an example of how to use `HttpClient` and `HttpRequestMessage` together: ```csharp var client = new HttpClient(); -var request = new HttpRequestMessage(HttpMethod.Post, "https://example.com"); // Method and RequestUri usage -var request = new HttpRequestMessage() // Alternative way to set RequestUri and Method +using var request = new HttpRequestMessage(HttpMethod.Post, "https://example.com"); // Method and RequestUri usage +using var request = new HttpRequestMessage() // Alternative way to set RequestUri and Method { RequestUri = new Uri("https://example.com"), Method = HttpMethod.Post @@ -215,7 +215,7 @@ using var response = await client.GetAsync(uri); var redirectedUri = response.RequestMessage.RequestUri; ``` -## Usage of SocketsHttpHandler and ConnectCallback +## Usage of `SocketsHttpHandler` and `ConnectCallback` The `ConnectCallback` property in `SocketsHttpHandler` allows developers to customize the process of establishing a TCP connection. This can be useful for scenarios where you need to control DNS resolution or apply specific socket options on the connection. By using `ConnectCallback`, you can intercept and modify the connection process before it is used by `HttpClient`. @@ -318,9 +318,9 @@ using var response = await client.GetAsync(uri); ### Example: Enable DNS round robin -DNS Round Robin is a technique used to distribute network traffic across multiple servers by rotating through a list of IP addresses associated with a single domain name. This helps in load balancing and improving the availability of services. When using HttpClient, you can implement DNS Round Robin by manually handling the DNS resolution and rotating through the IP addresses using the ConnectCallback property of SocketsHttpHandler. +DNS Round Robin is a technique used to distribute network traffic across multiple servers by rotating through a list of IP addresses associated with a single domain name. This helps in load balancing and improving the availability of services. When using `HttpClient`, you can implement DNS Round Robin by manually handling the DNS resolution and rotating through the IP addresses using the `ConnectCallback` property of `SocketsHttpHandler`. -To enable DNS Round Robin with HttpClient, you can use the ConnectCallback property to manually resolve the DNS entries and rotate through the IP addresses. Here's an example for `HttpWebRequest` and `HttpClient`: +To enable DNS Round Robin with `HttpClient`, you can use the `ConnectCallback` property to manually resolve the DNS entries and rotate through the IP addresses. Here's an example for `HttpWebRequest` and `HttpClient`: **Old code using `HttpWebRequest`**: @@ -343,11 +343,11 @@ For the implementation of `DnsRoundRobinConnector`, see [DnsRoundRobin.cs](https :::code source="../snippets/httpclient/Program.DnsRoundRobin.cs" id="DnsRoundRobinConnect"::: -### Example: Set SocketsHttpHandler properties +### Example: Set `SocketsHttpHandler` properties -SocketsHttpHandler is a powerful and flexible handler in .NET that provides advanced configuration options for managing HTTP connections. By setting various properties of SocketsHttpHandler, you can fine-tune the behavior of your HTTP client to meet specific requirements, such as performance optimization, security enhancements, and custom connection handling. +`SocketsHttpHandler` is a powerful and flexible handler in .NET that provides advanced configuration options for managing HTTP connections. By setting various properties of `SocketsHttpHandler`, you can fine-tune the behavior of your HTTP client to meet specific requirements, such as performance optimization, security enhancements, and custom connection handling. -Here's an example of how to configure SocketsHttpHandler with various properties and use it with HttpClient: +Here's an example of how to configure `SocketsHttpHandler` with various properties and use it with `HttpClient`: ```c# var cookieContainer = new CookieContainer(); @@ -374,11 +374,11 @@ using var response = await client.GetAsync(uri); This functionality is specific to certain platforms and is somewhat outdated. If you need a workaround, you can refer to [this section of the code](https://github.com/dotnet/runtime/blob/171f1a73a9f0fa77464995bcb893a59b9b98bc3d/src/libraries/System.Net.Requests/src/System/Net/HttpWebRequest.cs#L1678-L1684). -## Usage of Certificate and TLS-related properties in HttpClient +## Usage of Certificate and TLS-related properties in `HttpClient` When working with `HttpClient`, you may need to handle client certificates for various purposes, such as custom validation of server certificates or fetching the server certificate. `HttpClient` provides several properties and options to manage certificates effectively. -### Example: Check certificate revocation list with SocketsHttpHandler +### Example: Check certificate revocation list with `SocketsHttpHandler` The `CheckCertificateRevocationList` property in `SocketsHttpHandler.SslOptions` allows developers to enable or disable the check for certificate revocation lists (CRL) during SSL/TLS handshake. Enabling this property ensures that the client verifies whether the server's certificate has been revoked, enhancing the security of the connection. @@ -445,10 +445,10 @@ Mutual authentication, also known as two-way SSL or client certificate authentic To enable mutual authentication, follow these steps: - Load the client certificate. -- Configure the HttpClientHandler or SocketsHttpHandler to include the client certificate. +- Configure the `HttpClientHandler` or `SocketsHttpHandler` to include the client certificate. - Set up the server certificate validation callback if custom validation is needed. -Here's an example using SocketsHttpHandler: +Here's an example using `SocketsHttpHandler`: ```csharp var handler = new SocketsHttpHandler @@ -478,21 +478,21 @@ Headers play a crucial role in HTTP communication, providing essential metadata ### Set request headers -Request headers are used to provide additional information to the server about the request being made. Common use cases include specifying the content type, setting authentication tokens, and adding custom headers. You can set request headers using the `DefaultRequestHeaders` property of `HttpClient` or the Headers property of `HttpRequestMessage`. +Request headers are used to provide additional information to the server about the request being made. Common use cases include specifying the content type, setting authentication tokens, and adding custom headers. You can set request headers using the `DefaultRequestHeaders` property of `HttpClient` or the `Headers` property of `HttpRequestMessage`. #### Example: Set custom request headers -**Setting Default Custom Request Headers in HttpClient** +**Setting Default Custom Request Headers in `HttpClient`** ```csharp var client = new HttpClient(); client.DefaultRequestHeaders.Add("Custom-Header", "value"); ``` -**Setting Custom Request Headers in HttpRequestMessage** +**Setting Custom Request Headers in `HttpRequestMessage`** ```csharp -var request = new HttpRequestMessage(HttpMethod.Get, uri); +using var request = new HttpRequestMessage(HttpMethod.Get, uri); request.Headers.Add("Custom-Header", "value"); ``` @@ -503,7 +503,7 @@ For a comprehensive list of common properties available in [!WARNING] > `HttpClient` does not have built-in logic to cache responses. There is no workaround other than implementing all the caching yourself. Simply setting the headers will not achieve caching. @@ -585,7 +585,7 @@ For the implementation of `AddCacheControlHeaders`, see [AddCacheControlHeaders. ## Usage of buffering properties -When migrating from HttpWebRequest to `HttpClient`, it's important to understand the differences in how these two APIs handle buffering. +When migrating from `HttpWebRequest` to `HttpClient`, it's important to understand the differences in how these two APIs handle buffering. **Old code using `HttpWebRequest`**: @@ -601,16 +601,16 @@ request.AllowWriteStreamBuffering = false; // Default is `true`. In `HttpClient`, there are no direct equivalents to the `AllowWriteStreamBuffering` and `AllowReadStreamBuffering` properties. -HttpClient does not buffer request bodies on its own, instead delegating the responsibility to the `HttpContent` used. Contents like `StringContent` or `ByteArrayContent` are logically already buffered in memory, while using `StreamContent` will not incur any buffering by default. To force the content to be buffered, you may call `HttpContent.LoadIntoBufferAsync` before sending the request. Here's an example: +`HttpClient` does not buffer request bodies on its own, instead delegating the responsibility to the `HttpContent` used. Contents like `StringContent` or `ByteArrayContent` are logically already buffered in memory, while using `StreamContent` will not incur any buffering by default. To force the content to be buffered, you may call `HttpContent.LoadIntoBufferAsync` before sending the request. Here's an example: ```csharp HttpClient client = new HttpClient(); -HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri); +using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri); request.Content = new StreamContent(yourStream); await request.Content.LoadIntoBufferAsync(); -HttpResponseMessage response = await client.SendAsync(request); +using HttpResponseMessage response = await client.SendAsync(request); ``` In `HttpClient` read buffering is enabled by default. To avoid it, you may specify the `HttpCompletionOption.ResponseHeadersRead` flag, or use the `GetStreamAsync` helper. From cf629f16fe87b1f9c70d37e8bd7f03b5ccf918d7 Mon Sep 17 00:00:00 2001 From: "Meaghan Osagie (Lewis)" Date: Tue, 23 Sep 2025 11:00:43 -0700 Subject: [PATCH 2/4] Update docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md --- .../networking/http/httpclient-migrate-from-httpwebrequest.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md b/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md index 515ac1d826a5d..f319f123110f6 100644 --- a/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md +++ b/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md @@ -120,7 +120,7 @@ using HttpResponseMessage responseMessage = await client.PostAsync(uri, new Stri | `UseDefaultCredentials` | No direct equivalent API | [Example: Setting SocketsHttpHandler Properties](#example-set-socketshttphandler-properties). | | `UserAgent` | | [Example: Set Request Headers](#example-set-common-request-headers). | -## Migrate `ServicePoint`(`Manager`) usage +## Migrate `ServicePointManager` usage You should be aware that `ServicePointManager` is a static class, meaning that any changes made to its properties will have a global effect on all newly created `ServicePoint` objects within the application. For example, when you modify a property like `ConnectionLimit` or `Expect100Continue`, it impacts every new `ServicePoint` instance. From f60a11fa1b85768ff527513f6c180cab4343f3ad Mon Sep 17 00:00:00 2001 From: "Meaghan Osagie (Lewis)" Date: Tue, 23 Sep 2025 11:05:24 -0700 Subject: [PATCH 3/4] Update docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md --- .../networking/http/httpclient-migrate-from-httpwebrequest.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md b/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md index f319f123110f6..0632616eaaff7 100644 --- a/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md +++ b/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md @@ -558,7 +558,7 @@ And usage example of `TruncatedReadStream`: ### Example: Apply `CachePolicy` headers > [!WARNING] -> `HttpClient` does not have built-in logic to cache responses. There is no workaround other than implementing all the caching yourself. Simply setting the headers will not achieve caching. +> `HttpClient` doesn't have built-in logic to cache responses. There is no workaround other than implementing all the caching yourself. Simply setting the headers won't achieve caching. When migrating from `HttpWebRequest` to `HttpClient`, it's important to correctly handle cache-related headers such as `pragma` and `cache-control`. These headers control how responses are cached and retrieved, ensuring that your application behaves as expected in terms of performance and data freshness. From e45297428166f8e84d12efd4520ccb2bcc0f2df5 Mon Sep 17 00:00:00 2001 From: "Meaghan Osagie (Lewis)" Date: Tue, 23 Sep 2025 11:07:01 -0700 Subject: [PATCH 4/4] Update docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md --- .../networking/http/httpclient-migrate-from-httpwebrequest.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md b/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md index 0632616eaaff7..26ab3600e0398 100644 --- a/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md +++ b/docs/fundamentals/networking/http/httpclient-migrate-from-httpwebrequest.md @@ -601,7 +601,7 @@ request.AllowWriteStreamBuffering = false; // Default is `true`. In `HttpClient`, there are no direct equivalents to the `AllowWriteStreamBuffering` and `AllowReadStreamBuffering` properties. -`HttpClient` does not buffer request bodies on its own, instead delegating the responsibility to the `HttpContent` used. Contents like `StringContent` or `ByteArrayContent` are logically already buffered in memory, while using `StreamContent` will not incur any buffering by default. To force the content to be buffered, you may call `HttpContent.LoadIntoBufferAsync` before sending the request. Here's an example: +`HttpClient` doesn't buffer request bodies on its own, instead delegating the responsibility to the `HttpContent` used. Contents like `StringContent` or `ByteArrayContent` are logically already buffered in memory, while using `StreamContent` won't incur any buffering by default. To force the content to be buffered, you may call `HttpContent.LoadIntoBufferAsync` before sending the request. Here's an example: ```csharp HttpClient client = new HttpClient();