Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

c# - 如何发出HTTP POST Web请求(How to make HTTP POST web request)

Canonical

(典范)
How can I make an HTTP request and send some data using the POST method?

(如何使用 POST 方法 发出HTTP请求并发送一些数据 )

I can do GET request but have no idea how to make a POST .

(我可以执行GET请求,但不知道如何进行POST 。)

  ask by Hooch translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several ways to perform HTTP GET and POST requests:

(有几种方法可以执行HTTP GETPOST请求:)


Method A: HttpClient (Preferred) (方法A:HttpClient(首选))

This is a wrapper around HttpWebRequest .

(这是HttpWebRequest的包装。)

Compare with WebClient .

(WebClient比较 。)

Available in: .NET Framework 4.5+ , .NET Standard 1.1+ , .NET Core 1.0+ .

(适用于.NET Framework 4.5+ 、. .NET Standard 1.1+ 、. .NET Core 1.0+ 。)

Currently the preferred approach.

(目前是首选方法。)

Asynchronous.

(异步。)

Portable version for other platforms available via NuGet .

(可通过NuGet获得的其他平台的便携式版本。)

using System.Net.Http;

Setup (设定)

It is recommended to instantiate one HttpClient for your application's lifetime and share it.

(建议在应用程序的生命周期中实例化一个HttpClient并共享它。)

private static readonly HttpClient client = new HttpClient();

See HttpClientFactory for a Dependency Injection solution.

(有关依赖注入解决方案,请参见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"); 

Method B: 3rd-Party Libraries (方法B:第三方库)

  • RestSharp

    (RestSharp)

    Tried and tested library for interacting with REST APIs.

    (经过测试的库,用于与REST API进行交互。)

    Portable.

    (随身携带。)

    Available via NuGet .

    (可通过NuGet获得 。)

  • Flurl.Http

    (Flurl.Http)

    Newer library sporting a fluent API and testing helpers.

    (较新的库具有流畅的API和测试助手。)

    HttpClient under the hood.

    (深入了解HttpClient。)

    Portable.

    (随身携带。)

    Available via NuGet .

    (可通过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(); 

Method C: HttpWebRequest (Not recommended for new work) (方法C:HttpWebRequest(不建议用于新工作))

Available in: .NET Framework 1.1+ , .NET Standard 2.0+ , .NET Core 1.0+

(适用于.NET Framework 1.1+ .NET Standard 2.0+ 、. .NET Standard 2.0+ 、. .NET Core 1.0+)

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

Method D: WebClient (Not recommended for new work) (方法D:WebClient(不建议用于新工作))

This is a wrapper around HttpWebRequest .

(这是HttpWebRequest的包装。)

Compare with HttpClient .

(HttpClient进行比较 。)

Available in: .NET Framework 1.1+ , NET Standard 2.0+ , .NET Core 2.0+

(适用于.NET Framework 1.1+ NET Standard 2.0+ 、. 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"); } 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...