咨詢區(qū)
我會(huì)用 GET Request,但如何使用 Post Request 還得請(qǐng)教大家。
回答區(qū)
有多種方式可以使用 Http 的 GET 和 Post 請(qǐng)求。
A方法:HttpClient (推薦)
HttpClient 可用于 .NET Framework 4.5+, .NET Standard 1.1+,.NET Core 1.0+,當(dāng)前是最值得推薦的方式,它支持異步并且高性能,如果你是非常老的平臺(tái),還得需要從 Nuget 上安裝一下 System.Net.Http。
HttpClient 推薦的做法就是在應(yīng)用程序生命周期內(nèi)初始化一次,除非你有特殊的理由不這么做,使用方法如下:
private?static?readonly?HttpClient?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);var?responseString?=?await?response.Content.ReadAsStringAsync();
var?responseString?=?await?client.GetStringAsync("http://www.example.com/recepticle.aspx");
B方法:第三方包
RestSharp
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
這是一個(gè)比較新的工具包,擁有便捷易用的 API 接口,底層使用的是 HttpClient,而且支持移植,可以在 Nuget 上獲取。
var?responseString?=?await?"http://www.example.com/recepticle.aspx".PostUrlEncodedAsync(new?{?thing1?=?"hello",?thing2?=?"world"?}).ReceiveString();
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,性能較差,也沒有提供什么新功能。
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();
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+ 中可用。
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);
}
using?(var?client?=?new?WebClient())
{var?responseString?=?client.DownloadString("http://www.example.com/recepticle.aspx");
}
點(diǎn)評(píng)區(qū)
Evan Mulawski 大佬提到了 5 種方式,非常全面,值得學(xué)習(xí)了解,有一點(diǎn)要注意,在 .net core 2.1 種提供了一個(gè)新的 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
總結(jié)
以上是生活随笔為你收集整理的NET问答: C# 中有哪些 HttpPost 工具包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。