小编典典
有几种执行HTTP GET和POST请求的方法:
方法A:HttpClient(首选)
可用:.NET Framework 4.5+,.NET Standard 1.1+,.NET Core 1.0+。
目前,它是首选方法,并且具有异步性和高性能。在大多数情况下,请使用内置版本,但对于非常旧的平台,则提供了NuGet软件包。
using System.Net.Http;
设定
建议HttpClient在应用程序的生命周期中实例化一个实例,并共享它,除非您有特殊原因不这样做。
private static readonly HttpClient client = new HttpClient();
POST
var values = new Dictionary
{
{ “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:第三方库
POSTvar 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 string
var response2 = client.Post(request);
var name = response2.Data.Name;
这是一个更新的库,具有流畅的API,测试助手,在后台使用HttpClient且可移植。可通过NuGet获得。
using Flurl.Http;
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+。在.NET
Core中,它主要是为了兼容性-它可以包装HttpClient,性能较低且不会获得新功能。
using System.Net;
using System.Text; // For class Encoding
using System.IO; // For StreamReader
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(不建议用于新工作)
这是一个包装HttpWebRequest。与比较HttpClient。
可在:.NET Framework 1.1+,NET Standard 2.0+,.NET Core 2.0+
using System.Net;
using System.Collections.Specialized;
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”);
}
2020-05-19
今天的文章html发送post请求,如何发出HTTP POST Web请求分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/30301.html