You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -120,9 +120,9 @@ using HttpResponseMessage responseMessage = await client.PostAsync(uri, new Stri
120
120
|`UseDefaultCredentials`| No direct equivalent API |[Example: Setting SocketsHttpHandler Properties](#example-set-socketshttphandler-properties). |
121
121
|`UserAgent`|<xref:System.Net.Http.Headers.HttpRequestHeaders.UserAgent>|[Example: Set Request Headers](#example-set-common-request-headers). |
122
122
123
-
## Migrate ServicePoint(Manager) usage
123
+
## Migrate `ServicePointManager` usage
124
124
125
-
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.
125
+
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.
126
126
127
127
> [!WARNING]
128
128
> 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
181
181
|`CloseConnectionGroup`| No equivalent | No workaround |
182
182
|`SetTcpKeepAlive`| No direct equivalent API |[Usage of SocketsHttpHandler and ConnectCallback](#usage-of-socketshttphandler-and-connectcallback). |
183
183
184
-
## Usage of HttpClient and HttpRequestMessage properties
184
+
## Usage of `HttpClient` and `HttpRequestMessage` properties
185
185
186
-
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.
186
+
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.
187
187
188
-
### Example: Usage of HttpRequestMessage properties
188
+
### Example: Usage of `HttpRequestMessage` properties
189
189
190
-
Here's an example of how to use HttpClient and HttpRequestMessage together:
190
+
Here's an example of how to use `HttpClient` and `HttpRequestMessage` together:
191
191
192
192
```csharp
193
193
varclient=newHttpClient();
194
194
195
-
varrequest=newHttpRequestMessage(HttpMethod.Post, "https://example.com"); // Method and RequestUri usage
196
-
varrequest=newHttpRequestMessage() // Alternative way to set RequestUri and Method
195
+
usingvarrequest=newHttpRequestMessage(HttpMethod.Post, "https://example.com"); // Method and RequestUri usage
196
+
usingvarrequest=newHttpRequestMessage() // Alternative way to set RequestUri and Method
197
197
{
198
198
RequestUri=newUri("https://example.com"),
199
199
Method=HttpMethod.Post
@@ -215,7 +215,7 @@ using var response = await client.GetAsync(uri);
## Usage of SocketsHttpHandler and ConnectCallback
218
+
## Usage of `SocketsHttpHandler` and `ConnectCallback`
219
219
220
220
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`.
221
221
@@ -318,9 +318,9 @@ using var response = await client.GetAsync(uri);
318
318
319
319
### Example: Enable DNS round robin
320
320
321
-
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.
321
+
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`.
322
322
323
-
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`:
323
+
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`:
324
324
325
325
**Old code using `HttpWebRequest`**:
326
326
@@ -343,11 +343,11 @@ For the implementation of `DnsRoundRobinConnector`, see [DnsRoundRobin.cs](https
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.
348
+
`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.
349
349
350
-
Here's an example of how to configure SocketsHttpHandler with various properties and use it with HttpClient:
350
+
Here's an example of how to configure `SocketsHttpHandler` with various properties and use it with `HttpClient`:
351
351
352
352
```c#
353
353
varcookieContainer=newCookieContainer();
@@ -374,11 +374,11 @@ using var response = await client.GetAsync(uri);
374
374
375
375
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).
376
376
377
-
## Usage of Certificate and TLS-related properties in HttpClient
377
+
## Usage of Certificate and TLS-related properties in `HttpClient`
378
378
379
379
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.
380
380
381
-
### Example: Check certificate revocation list with SocketsHttpHandler
381
+
### Example: Check certificate revocation list with `SocketsHttpHandler`
382
382
383
383
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.
384
384
@@ -445,10 +445,10 @@ Mutual authentication, also known as two-way SSL or client certificate authentic
445
445
To enable mutual authentication, follow these steps:
446
446
447
447
- Load the client certificate.
448
-
- Configure the HttpClientHandler or SocketsHttpHandler to include the client certificate.
448
+
- Configure the `HttpClientHandler` or `SocketsHttpHandler` to include the client certificate.
449
449
- Set up the server certificate validation callback if custom validation is needed.
450
450
451
-
Here's an example using SocketsHttpHandler:
451
+
Here's an example using `SocketsHttpHandler`:
452
452
453
453
```csharp
454
454
varhandler=newSocketsHttpHandler
@@ -478,21 +478,21 @@ Headers play a crucial role in HTTP communication, providing essential metadata
478
478
479
479
### Set request headers
480
480
481
-
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`.
481
+
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`.
482
482
483
483
#### Example: Set custom request headers
484
484
485
-
**Setting Default Custom Request Headers in HttpClient**
485
+
**Setting Default Custom Request Headers in `HttpClient`**
@@ -503,7 +503,7 @@ For a comprehensive list of common properties available in <xref:System.Net.Http
503
503
504
504
To set common request headers in `HttpRequestMessage`, you can use the `Headers` property of the `HttpRequestMessage` object. This property provides access to the `HttpRequestHeaders` collection, where you can add or modify headers as needed.
505
505
506
-
**Setting Common Default Request Headers in HttpClient**
506
+
**Setting Common Default Request Headers in `HttpClient`**
507
507
508
508
```csharp
509
509
usingSystem.Net.Http.Headers;
@@ -512,12 +512,12 @@ var client = new HttpClient();
### Example: Set MaximumErrorResponseLength in HttpClient
546
+
### Example: Set `MaximumErrorResponseLength` in `HttpClient`
547
547
548
548
The `MaximumErrorResponseLength` usage allows developers to specify the maximum length of the error response content that the handler will buffer. This is useful for controlling the amount of data that is read and stored in memory when an error response is received from the server. By using this technique, you can prevent excessive memory usage and improve the performance of your application when handling large error responses.
549
549
@@ -555,10 +555,10 @@ And usage example of `TruncatedReadStream`:
> `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.
561
+
> `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.
562
562
563
563
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.
564
564
@@ -585,7 +585,7 @@ For the implementation of `AddCacheControlHeaders`, see [AddCacheControlHeaders.
585
585
586
586
## Usage of buffering properties
587
587
588
-
When migrating from HttpWebRequest to `HttpClient`, it's important to understand the differences in how these two APIs handle buffering.
588
+
When migrating from `HttpWebRequest` to `HttpClient`, it's important to understand the differences in how these two APIs handle buffering.
In `HttpClient`, there are no direct equivalents to the `AllowWriteStreamBuffering` and `AllowReadStreamBuffering` properties.
603
603
604
-
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:
604
+
`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:
In `HttpClient` read buffering is enabled by default. To avoid it, you may specify the `HttpCompletionOption.ResponseHeadersRead` flag, or use the `GetStreamAsync` helper.
0 commit comments