.NET sends a detailed explanation of the http post request method containing the text data
This article introduces several methods to send http post requests and pass the text data.
For the .NET CORE and updated version of the .NET Framework, HTTPClient was the preferred HTTP request method. It provides asynchronous and high -performance operations.
<.> 2. The third party library
using System.Net.Http; var client = new HttpClient(); var values = new Dictionary<string, string> { { "thing1", "hello" }, { "thing2", "world" } }; var content = new FormUrlEncodedContent(values); var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
using RestSharp; var client = new RestClient("http://example.com"); var request = new RestRequest("resource/{id}"); request.AddParameter("thing1", "Hello"); request.AddParameter("thing2", "world"); var response = client.Post(request);
<.> 3. httpwebrequest (not recommended for new projects)
using Flurl.Http; var responseString = await "http://www.example.com/recepticle.aspx" .PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" }) .ReceiveString();
<.> 4. Webclient (not recommended for new projects)
using System.Net; using System.Text; var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx"); var postData = "thing1=" + Uri.EscapeDataString("hello"); postData += "&thing2=" + Uri.EscapeDataString("world"); var data = Encoding.ASCII.GetBytes(postData); using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } var response = request.GetResponse();
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx"); var response = request.GetResponse();
This article compares a variety of .NET to send HTTP Post requests, and it is recommended to use HTTPClient. For new projects, it is strongly recommended to use HTTPClient, because it is more modern, more performance, and supports asynchronous operations.
The above is the detailed content of How to Send HTTP POST Requests with Body Data in .NET?. For more information, please follow other related articles on the PHP Chinese website!