日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

NET问答: C# 中有哪些 HttpPost 工具包

發(fā)布時(shí)間:2023/12/4 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NET问答: C# 中有哪些 HttpPost 工具包 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

咨詢區(qū)

  • Hooch

我會(huì)用 GET Request,但如何使用 Post Request 還得請教大家。

回答區(qū)

  • Evan Mulawski

有多種方式可以使用 Http 的 GET 和 Post 請求。

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();
  • 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

這是一個(gè)比較新的工具包,擁有便捷易用的 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"); }

點(diǎn)評區(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ò),歡迎將生活随笔推薦給好友。