Send HTTP POST request in .NET
Send HTTP POST request in .NET
Send HTTP POST request in .NET
Re: Send HTTP POST request in .NET
There are several ways to perform HTTP [GET](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods) and [POST](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods) requests:
Method A: HttpClient (Preferred)
Available in: .NET Framework 4.5+, .NET Standard 1.1+, and .NET Core 1.0+.
It is currently the preferred approach, and is asynchronous and high performance. Use the built-in version in most cases, but for very old platforms there is a [NuGet package](https://www.nuget.org/packages/System.Net.Http).
```
using System.Net.Http;
```
Setup
[It is recommended](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/) to instantiate one `HttpClient` for your application's lifetime and share it unless you have a specific reason not to.
```
private static readonly HttpClient client = new HttpClient();
```
See [`HttpClientFactory`](https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#what-is-httpclientfactory) for a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) solution.
-
POST
```
var values = new Dictionary
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
```
-
GET
```
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
```
Method B: Third-Party Libraries
***[RestSharp](https://github.com/restsharp/RestSharp)***
-
POST
```
var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest("resource/{id}");
request.AddParameter("thing1", "Hello");
request.A
*(Réponse tronquée)*
Method A: HttpClient (Preferred)
Available in: .NET Framework 4.5+, .NET Standard 1.1+, and .NET Core 1.0+.
It is currently the preferred approach, and is asynchronous and high performance. Use the built-in version in most cases, but for very old platforms there is a [NuGet package](https://www.nuget.org/packages/System.Net.Http).
```
using System.Net.Http;
```
Setup
[It is recommended](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/) to instantiate one `HttpClient` for your application's lifetime and share it unless you have a specific reason not to.
```
private static readonly HttpClient client = new HttpClient();
```
See [`HttpClientFactory`](https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#what-is-httpclientfactory) for a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) solution.
-
POST
```
var values = new Dictionary
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
```
-
GET
```
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
```
Method B: Third-Party Libraries
***[RestSharp](https://github.com/restsharp/RestSharp)***
-
POST
```
var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest("resource/{id}");
request.AddParameter("thing1", "Hello");
request.A
*(Réponse tronquée)*