网站建设中图片多少钱,网络设计开发网站,如何免费注册网站,教学方面网站建设咨询区 Hooch#xff1a;我会用 GET Request#xff0c;但如何使用 Post Request 还得请教大家。回答区 Evan Mulawski#xff1a;有多种方式可以使用 Http 的 GET 和 Post 请求。A方法#xff1a;HttpClient (推荐)HttpClient 可用于 .NET Framework 4.5, .NET Standard 1… 咨询区 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 Dictionarystring, 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方法第三方包RestSharpPOSTvar 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.PostPerson(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 的各种不足有兴趣可以看下 MSDNhttps://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