Here is an example of sending a POST request with data and reading the response using HttpClient class in C#:
using System; using System.Net.Http; using System.Threading.Tasks; public class Program { public static async Task Main(string[] args) { using var client = new HttpClient(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("key1", "value1"), new KeyValuePair<string, string>("key2", "value2"), }); var response = await client.PostAsync("https://example.com/api", data); if (response.IsSuccessStatusCode) { var responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } else { Console.WriteLine("Failed with status code: " + response.StatusCode); } } } In this example, we create an instance of HttpClient and use it to send a POST request to https://example.com/api. We pass some data as FormUrlEncodedContent in the request body. We then check if the response was successful, and if so, read the response body as a string using ReadAsStringAsync method of the response content object. If the response was not successful, we print an error message along with the status code of the response.
"Simplest way to send a POST request in .NET with data"
HttpClient class.using (HttpClient client = new HttpClient()) { var postData = new StringContent("key1=value1&key2=value2", Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", postData); string result = await response.Content.ReadAsStringAsync(); } ".NET HttpClient POST request with JSON data"
HttpClient class for a straightforward implementation.using (HttpClient client = new HttpClient()) { var jsonData = new StringContent("{\"key1\":\"value1\",\"key2\":\"value2\"}", Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", jsonData); string result = await response.Content.ReadAsStringAsync(); } "POST request with form data in .NET Core"
HttpClient.using (HttpClient client = new HttpClient()) { var formData = new MultipartFormDataContent(); formData.Add(new StringContent("value1"), "key1"); formData.Add(new StringContent("value2"), "key2"); HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", formData); string result = await response.Content.ReadAsStringAsync(); } ".NET WebClient POST request example"
WebClient class in .NET for a concise and easy-to-understand approach.using (WebClient client = new WebClient()) { var postData = new NameValueCollection() { { "key1", "value1" }, { "key2", "value2" } }; byte[] responseBytes = client.UploadValues("https://example.com/api/resource", "POST", postData); string result = Encoding.UTF8.GetString(responseBytes); } ".NET Core HTTP POST request with headers"
HttpClient class.using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer YourAccessToken"); var postData = new StringContent("key1=value1&key2=value2", Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", postData); string result = await response.Content.ReadAsStringAsync(); } ".NET Core POST request with timeout setting"
HttpClient class to manage network request duration.using (HttpClient client = new HttpClient()) { client.Timeout = TimeSpan.FromSeconds(10); var postData = new StringContent("key1=value1&key2=value2", Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", postData); string result = await response.Content.ReadAsStringAsync(); } ".NET Core POST request with authentication"
HttpClient class.using (HttpClient client = new HttpClient()) { var credentials = new NetworkCredential("username", "password"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{credentials.UserName}:{credentials.Password}"))); var postData = new StringContent("key1=value1&key2=value2", Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", postData); string result = await response.Content.ReadAsStringAsync(); } "Handling exceptions in .NET POST request"
try { using (HttpClient client = new HttpClient()) { var postData = new StringContent("key1=value1&key2=value2", Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", postData); response.EnsureSuccessStatusCode(); string result = await response.Content.ReadAsStringAsync(); } } catch (HttpRequestException ex) { // Handle exception } ".NET Core POST request with TLS/SSL configuration"
HttpClient class.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; using (HttpClient client = new HttpClient()) { var postData = new StringContent("key1=value1&key2=value2", Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", postData); string result = await response.Content.ReadAsStringAsync(); } ".NET Core POST request with Bearer token"
HttpClient class.using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "YourAccessToken"); var postData = new StringContent("key1=value1&key2=value2", Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", postData); string result = await response.Content.ReadAsStringAsync(); } conflict jenkins-agent facelets sdk solidity cllocationmanager floating-point-precision shapely lodash logitech