日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

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

咨詢區

  • 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

總結

以上是生活随笔為你收集整理的NET问答: C# 中有哪些 HttpPost 工具包的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。