You can send an HTTP POST request in .NET using the HttpClient class, which is part of the System.Net.Http namespace. Here's an example:
using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { string url = "https://example.com/api/create"; HttpClient httpClient = new HttpClient(); var data = new { Name = "John", Age = 30 }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(data); var content = new StringContent(json, Encoding.UTF8, "application/json"); HttpResponseMessage response = await httpClient.PostAsync(url, content); if (response.IsSuccessStatusCode) { string responseJson = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseJson); } else { Console.WriteLine("HTTP Error: " + response.StatusCode.ToString()); } } } In this example, we create a HttpClient object and set the URL we want to send the POST request to. We then create an anonymous object containing the data we want to send in the request body, serialize it to JSON, and create a StringContent object from it. The StringContent object is then passed to the PostAsync method of the HttpClient object, along with the URL. The PostAsync method sends the HTTP POST request and returns a HttpResponseMessage object.
We then check whether the response was successful by checking the IsSuccessStatusCode property of the HttpResponseMessage object. If it was successful, we read the response body as a string and output it to the console. Otherwise, we output an error message that includes the HTTP status code.
Note that the HttpClient class should be created once and reused for multiple requests to the same host. Also, in this example, we use the Newtonsoft.Json library to serialize the data to JSON, but you can use other JSON libraries if you prefer.
"Simple HTTP POST request in C#"
HttpClient class.using System; using System.Net.Http; using System.Threading.Tasks; var httpClient = new HttpClient(); var apiEndpoint = "https://your-api-endpoint"; var content = new StringContent("your-post-data"); var response = await httpClient.PostAsync(apiEndpoint, content); // Handle the response as needed "C# HTTP POST request with JSON content"
HttpClient class.using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; var httpClient = new HttpClient(); var apiEndpoint = "https://your-api-endpoint"; var jsonContent = new StringContent("{'key':'value'}", Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(apiEndpoint, jsonContent); // Handle the response as needed "C# HTTP POST request with form data"
HttpClient class.using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; var httpClient = new HttpClient(); var apiEndpoint = "https://your-api-endpoint"; var formData = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("key1", "value1"), new KeyValuePair<string, string>("key2", "value2") }; var formContent = new FormUrlEncodedContent(formData); var response = await httpClient.PostAsync(apiEndpoint, formContent); // Handle the response as needed "C# HTTP POST request with headers"
HttpClient class.using System; using System.Net.Http; using System.Threading.Tasks; var httpClient = new HttpClient(); var apiEndpoint = "https://your-api-endpoint"; var content = new StringContent("your-post-data"); // Add custom headers httpClient.DefaultRequestHeaders.Add("Custom-Header", "header-value"); var response = await httpClient.PostAsync(apiEndpoint, content); // Handle the response as needed "C# HTTP POST request with timeout"
HttpClient class.using System; using System.Net.Http; using System.Threading.Tasks; var httpClient = new HttpClient(); var apiEndpoint = "https://your-api-endpoint"; var content = new StringContent("your-post-data"); httpClient.Timeout = TimeSpan.FromSeconds(30); // Set timeout var response = await httpClient.PostAsync(apiEndpoint, content); // Handle the response as needed "C# HTTP POST request with cancellation token"
HttpClient class.using System; using System.Net.Http; using System.Threading; using System.Threading.Tasks; var httpClient = new HttpClient(); var apiEndpoint = "https://your-api-endpoint"; var content = new StringContent("your-post-data"); var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(10)); // Set cancellation token timeout var response = await httpClient.PostAsync(apiEndpoint, content, cancellationTokenSource.Token); // Handle the response as needed "C# HTTP POST request with proxy settings"
HttpClient class.using System.Net; using System.Net.Http; using System.Threading.Tasks; var httpClientHandler = new HttpClientHandler { Proxy = new WebProxy("http://your-proxy-server:8080"), UseProxy = true }; var httpClient = new HttpClient(httpClientHandler); var apiEndpoint = "https://your-api-endpoint"; var content = new StringContent("your-post-data"); var response = await httpClient.PostAsync(apiEndpoint, content); // Handle the response as needed "C# HTTP POST request with response deserialization"
HttpClient class.using System; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json; var httpClient = new HttpClient(); var apiEndpoint = "https://your-api-endpoint"; var content = new StringContent("your-post-data"); var response = await httpClient.PostAsync(apiEndpoint, content); if (response.IsSuccessStatusCode) { var responseData = await response.Content.ReadAsStringAsync(); var deserializedObject = JsonConvert.DeserializeObject<YourResponseType>(responseData); // Handle the deserialized object } else { // Handle error } "C# HTTP POST request with SSL/TLS"
HttpClient class.using System; using System.Net.Http; using System.Threading.Tasks; var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true }; var httpClient = new HttpClient(httpClientHandler); var apiEndpoint = "https://your-api-endpoint"; var content = new StringContent("your-post-data"); var response = await httpClient.PostAsync(apiEndpoint, content); // Handle the response as needed "C# HTTP POST request with multipart content"
HttpClient class.using System.Net.Http; using System.Threading.Tasks; var httpClient = new HttpClient(); var apiEndpoint = "https://your-api-endpoint"; var multipartContent = new MultipartFormDataContent(); // Add parts to multipart content (e.g., file upload) multipartContent.Add(new StringContent("your-value"), "your-key"); multipartContent.Add(new StreamContent(yourFileStream), "file", "filename.txt"); var response = await httpClient.PostAsync(apiEndpoint, multipartContent); // Handle the response as needed keras cache-control select-for-update wordpress-theming laravel-authentication autohotkey pypdf squirrel-sql directive primeng-turbotable