小编典典

如何发出 HTTP POST 网络请求

all

Canonical
如何使用该 POST 方法发出 HTTP 请求并发送一些数据?

我可以GET提出请求,但我不知道如何提出POST请求。


阅读 275

收藏
2022-02-25

共1个答案

小编典典

有几种方法可以执行 HTTPGETPOST请求:


方法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();

查看依赖注入HttpClientFactory解决方案。


  • 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 string
    var response2 = client.Post(request);
    var name = response2.Data.Name;

Flurl.Http

它是一个较新的库,具有流畅的 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
中,它主要是为了兼容性——它是 wrapsHttpClient的,性能较低,并且不会获得新功能。

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”);
    }

2022-02-25