.NETプラットフォームは、httpの投稿リクエストを送信するためにさまざまな方法を送信します詳細
推奨方法:httpclient
.NETでは、
HttpClient
メソッド2:サードパーティライブラリ
<code class="language-csharp">// 初始化 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();</code>
RestSharpは、人気のある第3パーティHTTPリクエストライブラリで、便利で簡単なAPIとリッチ機能を提供します。
<code class="language-csharp">// POST 请求 var client = new RestClient("http://example.com"); var request = new RestRequest("resource/{id}"); request.AddParameter("thing1", "Hello"); request.AddParameter("thing2", "world"); var response = client.Post(request); var content = response.Content; // 原始字符串内容</code>flurl.httpは、滑らかなAPIとポータブルを備えた新しいライブラリです。
メソッド3:httpwebrequest
放棄された
<code class="language-csharp">// POST 请求 var responseString = await "http://www.example.com/recepticle.aspx" .PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" }) .ReceiveString();</code>
は、新しいプロジェクトには推奨されていません。そのパフォーマンスは低く、機能が
ほど良くないためです。メソッド4:webclient
放棄されたHttpWebRequest
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();
パッケージングデバイスであり、通常は新しいプロジェクトには推奨されません。
以上がHTTPCLIENT、RESTSHARP、およびその他の方法を使用して.NETでHTTP POSTリクエストを送信するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。