咨询区
Hooch:
我会用 GET Request
,但如何使用 Post Request
还得请教大家。
回答区
Evan Mulawski:
有多种方式可以使用 Http 的 GET 和 Post 请求。
A方法:HttpClient (推荐)
HttpClient 可用于 .NET Framework 4.5+
, .NET Standard 1.1+
,.NET Core 1.0+
,当前是最值得推荐的方式,它支持异步并且高性能,如果你是非常老的平台,还得需要从 Nuget 上安装一下 System.Net.Http
。
HttpClient 推荐的做法就是在应用程序生命周期内初始化一次,除非你有特殊的理由不这么做,使用方法如下:
private static readonly HttpClient client = new HttpClient();
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");
B方法:第三方包
RestSharp
POST
var client = new RestClient("http://example.com");// client.Authenticator = new HttpBasicAuthenticator(username, password);var request = new RestRequest("resource/{id}");request.AddParameter("thing1", "Hello");request.AddParameter("thing2", "world");request.AddHeader("header", "value");request.AddFile("file", path);var response = client.Post(request);var content = response.Content; // Raw content as stringvar response2 = client.Post<Person>(request);var name = response2.Data.Name;
Flurl.Http
这是一个比较新的工具包,拥有便捷易用的 API 接口,底层使用的是 HttpClient,而且支持移植,可以在 Nuget 上获取。
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();
C方法:HttpWebRequest (不推荐)
它可用于 .NET Framework 1.1+
, .NET Standard 2.0+
,.NET Core 1.0+
,在 .netcore 中仅仅是为了兼容而存在的,它封装了 HttpClient,性能较差,也没有提供什么新功能。
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();
D方法:WebClient (不推荐)
WebClient 封装了 HttpWebRequest,在 .NET Framework 1.1+
,NET Standard 2.0+
,.NET Core 2.0+
中可用。
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");
}
点评区
Evan Mulawski
大佬提到了 5 种方式,非常全面,值得学习了解,有一点要注意,在 .net core 2.1
种提供了一个新的 HttpClientFacotry
类,就就是用来解决 HttpClient 的各种不足,有兴趣可以看下 MSDN:https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#what-is-httpclientfactory
原文链接:https://stackoverflow.com/questions/4015324/how-to-make-an-http-post-web-request