Canonical
(典范)
How can I make an HTTP request and send some data using thePOST
method?(如何使用
POST
方法 发出HTTP请求并发送一些数据 ?)
I can do GET
request but have no idea how to make a POST
.
(我可以执行GET
请求,但不知道如何进行POST
。)
Canonical
(典范)
How can I make an HTTP request and send some data using thePOST
method?(如何使用
POST
方法 发出HTTP请求并发送一些数据 ?)
I can do GET
request but have no idea how to make a POST
.
(我可以执行GET
请求,但不知道如何进行POST
。)
There are several ways to perform HTTP GET
and POST
requests:
(有几种方法可以执行HTTP GET
和POST
请求:)
This is a wrapper around HttpWebRequest
.
(这是HttpWebRequest
的包装。)
WebClient
. (与WebClient
比较 。)
Available in: .NET Framework 4.5+
, .NET Standard 1.1+
, .NET Core 1.0+
.
(适用于.NET Framework 4.5+
、. .NET Standard 1.1+
、. .NET Core 1.0+
。)
Currently the preferred approach.
(目前是首选方法。)
Asynchronous.(异步。)
Portable version for other platforms available via NuGet .(可通过NuGet获得的其他平台的便携式版本。)
using System.Net.Http;
It is recommended to instantiate one HttpClient
for your application's lifetime and share it.
(建议在应用程序的生命周期中实例化一个HttpClient
并共享它。)
private static readonly HttpClient client = new HttpClient();
See HttpClientFactory
for a Dependency Injection solution.
(有关依赖注入解决方案,请参见HttpClientFactory
。)
POST
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); var responseString = await response.Content.ReadAsStringAsync();
GET
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
Tried and tested library for interacting with REST APIs.
(经过测试的库,用于与REST API进行交互。)
Portable.(随身携带。)
Available via NuGet .(可通过NuGet获得 。)
Newer library sporting a fluent API and testing helpers.
(较新的库具有流畅的API和测试助手。)
HttpClient under the hood.(深入了解HttpClient。)
Portable.(随身携带。)
Available via NuGet .(可通过NuGet获得 。)
using Flurl.Http;
POST
var responseString = await "http://www.example.com/recepticle.aspx" .PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" }) .ReceiveString();
GET
var responseString = await "http://www.example.com/recepticle.aspx" .GetStringAsync();
Available in: .NET Framework 1.1+
, .NET Standard 2.0+
, .NET Core 1.0+
(适用于.NET Framework 1.1+
.NET Standard 2.0+
、. .NET Standard 2.0+
、. .NET Core 1.0+
)
using System.Net;
using System.Text; // for class Encoding
using System.IO; // for StreamReader
POST
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); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
GET
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx"); var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
This is a wrapper around HttpWebRequest
.
(这是HttpWebRequest
的包装。)
HttpClient
. (与HttpClient
进行比较 。)
Available in: .NET Framework 1.1+
, NET Standard 2.0+
, .NET Core 2.0+
(适用于.NET Framework 1.1+
NET Standard 2.0+
、. NET Standard 2.0+
、. .NET Core 2.0+
)
using System.Net;
using System.Collections.Specialized;
POST
using (var client = new WebClient()) { var values = new NameValueCollection(); values["thing1"] = "hello"; values["thing2"] = "world"; var response = client.UploadValues("http://www.example.com/recepticle.aspx", values); var responseString = Encoding.Default.GetString(response); }
GET
using (var client = new WebClient()) { var responseString = client.DownloadString("http://www.example.com/recepticle.aspx"); }