There are several ways to perform HTTP GET
and POST
requests:
(有几种方法可以执行HTTP GET
和POST
请求:)
Method A: HttpClient (Preferred) (方法A:HttpClient(首选))
This is a wrapper around HttpWebRequest
.
(这是HttpWebRequest
的包装。)
Compare with 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;
Setup (设定)
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");
Method B: 3rd-Party Libraries (方法B:第三方库)
RestSharp
(RestSharp)
Tried and tested library for interacting with REST APIs.
(经过测试的库,用于与REST API进行交互。)
Portable. (随身携带。)
Available via NuGet . (可通过NuGet获得 。)
Flurl.Http
(Flurl.Http)
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();
Method C: HttpWebRequest (Not recommended for new work) (方法C:HttpWebRequest(不建议用于新工作))
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();
Method D: WebClient (Not recommended for new work) (方法D:WebClient(不建议用于新工作))
This is a wrapper around HttpWebRequest
.
(这是HttpWebRequest
的包装。)
Compare with 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"); }
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…