我如何能使一个HTTP POST请求和发送数据的主体?
当前回答
为什么这不是完全无关紧要的?执行请求并不是处理结果。而且似乎还涉及到一些。net Bug——参见HttpClient中的Bug。GetAsync应该抛出WebException,而不是TaskCanceledException
我最终得到了这样的代码:
static async Task<(bool Success, WebExceptionStatus WebExceptionStatus, HttpStatusCode? HttpStatusCode, string ResponseAsString)> HttpRequestAsync(HttpClient httpClient, string url, string postBuffer = null, CancellationTokenSource cts = null) {
try {
HttpResponseMessage resp = null;
if (postBuffer is null) {
resp = cts is null ? await httpClient.GetAsync(url) : await httpClient.GetAsync(url, cts.Token);
} else {
using (var httpContent = new StringContent(postBuffer)) {
resp = cts is null ? await httpClient.PostAsync(url, httpContent) : await httpClient.PostAsync(url, httpContent, cts.Token);
}
}
var respString = await resp.Content.ReadAsStringAsync();
return (resp.IsSuccessStatusCode, WebExceptionStatus.Success, resp.StatusCode, respString);
} catch (WebException ex) {
WebExceptionStatus status = ex.Status;
if (status == WebExceptionStatus.ProtocolError) {
// Get HttpWebResponse so that you can check the HTTP status code.
using (HttpWebResponse httpResponse = (HttpWebResponse)ex.Response) {
return (false, status, httpResponse.StatusCode, httpResponse.StatusDescription);
}
} else {
return (false, status, null, ex.ToString());
}
// https://devblogs.microsoft.com/dotnet/net-5-new-networking-improvements/
} catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException) {
return (false, ex.ToString(), null, WebExceptionStatus.Timeout);
} catch (TaskCanceledException ex) {
return (false, ex.ToString(), null, WebExceptionStatus.RequestCanceled);
} catch (Exception ex) {
return (false, WebExceptionStatus.UnknownError, null, ex.ToString());
}
}
这将根据postBuffer是否为空来执行GET或POST操作。
如果Success为true,响应将在ResponseAsString中。
如果Success为false,你可以检查WebExceptionStatus, HttpStatusCode和ResponseAsString,看看哪里出了问题。
其他回答
当使用Windows.Web.Http命名空间时,对于POST而不是FormUrlEncodedContent,我们编写HttpFormUrlEncodedContent。同样,响应类型为HttpResponseMessage。其余的就像Evan Mulawski写的那样。
执行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();
请参阅HttpClientFactory以获得依赖注入解决方案。
帖子 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 =等待response.Content.ReadAsStringAsync(); 得到 var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
方法B:第三方库
休息夏普
帖子 var client = new RestClient("http://example.com"); / /客户端。Authenticator =新的HttpBasicAuthenticator(用户名,密码); var request = new RestRequest("resource/{id}"); 请求。AddParameter(“thing1”、“你好”); 请求。AddParameter(“事件”、“世界”); 请求。AddHeader(“标题”、“价值”); 请求。AddFile(“文件”,路径); var response = client.Post(请求); var content = response.Content;//原始内容为字符串 var response2 = client.Post<Person>(request); var name = response2.Data.Name;
Flurl。Http
它是一个更新的库,拥有一个流畅的API,测试助手,在底层使用HttpClient,并且是可移植的。它可以通过NuGet获得。
using Flurl.Http;
帖子 var responseString = await“http://www.example.com/recepticle.aspx” .PostUrlEncodedAsync(new {thing1 = "hello", thing2 = "world"}) .ReceiveString (); 得到 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
发布 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); 请求。方法 =“开机自检”; 请求。ContentType = “application/x-www-form-urlencoded”; 请求。内容长度 = 数据。长度; 使用 (var 流 = 请求。GetRequestStream()) { 流。写入(数据, 0, 数据.长度); } var response = (HttpWebResponse)request.获取响应(); var responseString = new StreamReader(response.GetResponseStream())。ReadToEnd(); 获取 var request = (HttpWebRequest)WebRequest.Create(“http://www.example.com/recepticle.aspx”); var response = (HttpWebResponse)request.获取响应(); var responseString = new StreamReader(response.GetResponseStream())。ReadToEnd();
方法D: WebClient(不推荐新作品)
这是一个HttpWebRequest的包装器。与HttpClient比较。
支持。NET Framework 1.1+, NET Standard 2.0+和。NET Core 2.0+。
在某些情况下…NET Framework 4.5-4.8),如果你需要同步地做一个HTTP请求,WebClient仍然可以使用。
using System.Net;
using System.Collections.Specialized;
帖子 使用(var client = new WebClient()) { var values = new NameValueCollection(); 值["thing1"] = "hello"; Values ["thing2"] = "world"; var response = client.UploadValues("http://www.example.com/recepticle.aspx", values); var responseString = Encoding.Default.GetString(response); } 得到 使用(var client = new WebClient()) { var responseString = client.DownloadString("http://www.example.com/recepticle.aspx"); }
MSDN有一个样本。
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestPostExample
{
public static void Main()
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
}
}
这是一个完整的JSON格式发送/接收数据的工作示例,我使用Visual Studio 2013 Express Edition:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
namespace ConsoleApplication1
{
class Customer
{
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
}
public class Program
{
private static readonly HttpClient _Client = new HttpClient();
private static JavaScriptSerializer _Serializer = new JavaScriptSerializer();
static void Main(string[] args)
{
Run().Wait();
}
static async Task Run()
{
string url = "http://www.example.com/api/Customer";
Customer cust = new Customer() { Name = "Example Customer", Address = "Some example address", Phone = "Some phone number" };
var json = _Serializer.Serialize(cust);
var response = await Request(HttpMethod.Post, url, json, new Dictionary<string, string>());
string responseText = await response.Content.ReadAsStringAsync();
List<YourCustomClassModel> serializedResult = _Serializer.Deserialize<List<YourCustomClassModel>>(responseText);
Console.WriteLine(responseText);
Console.ReadLine();
}
/// <summary>
/// Makes an async HTTP Request
/// </summary>
/// <param name="pMethod">Those methods you know: GET, POST, HEAD, etc...</param>
/// <param name="pUrl">Very predictable...</param>
/// <param name="pJsonContent">String data to POST on the server</param>
/// <param name="pHeaders">If you use some kind of Authorization you should use this</param>
/// <returns></returns>
static async Task<HttpResponseMessage> Request(HttpMethod pMethod, string pUrl, string pJsonContent, Dictionary<string, string> pHeaders)
{
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.Method = pMethod;
httpRequestMessage.RequestUri = new Uri(pUrl);
foreach (var head in pHeaders)
{
httpRequestMessage.Headers.Add(head.Key, head.Value);
}
switch (pMethod.Method)
{
case "POST":
HttpContent httpContent = new StringContent(pJsonContent, Encoding.UTF8, "application/json");
httpRequestMessage.Content = httpContent;
break;
}
return await _Client.SendAsync(httpRequestMessage);
}
}
}
这是一个HTTPS web请求的例子。可以在PHP脚本中回显任何结果。最后,PHP回显字符串将在c#客户端显示为警报。
string url = "https://mydomain.ir/test1.php";
StringBuilder postData = new StringBuilder();
postData.Append(String.Format("{0}={1}&", HttpUtility.HtmlEncode("username"), HttpUtility.HtmlEncode("ali")));
postData.Append(String.Format("{0}={1}", HttpUtility.HtmlEncode("password"), HttpUtility.HtmlEncode("123456789")));
StringContent myStringContent = new StringContent(postData.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded");
HttpClient client = new HttpClient();
HttpResponseMessage message = client.PostAsync(url, myStringContent).GetAwaiter().GetResult();
string responseContent = message.Content.ReadAsStringAsync().GetAwaiter().GetResult();
DisplayAlert("Your Feedback", responseContent, "OK");
PHP服务器端:
<?php
if (isset($_POST["username"]) && $_POST["username"] == "ali") {
echo "Yes, hi Ali";
}
else {
echo "No, where is Ali?";
}
?>
结果将是“Yes, hi Ali”。
这是为Xamarin形式。对于一个c# .NET应用程序,将DisplayAlert替换为:
MessageBox.show(responseContent);
推荐文章
- Linq-to-Entities Join vs GroupJoin
- 为什么字符串类型的默认值是null而不是空字符串?
- 在list中获取不同值的列表
- 组合框:向项目添加文本和值(无绑定源)
- 我如何捕捉Ajax查询后错误?
- AutoMapper:“忽略剩下的?”
- 如何为ASP.net/C#应用程序配置文件值中的值添加&号
- 从System.Drawing.Bitmap中加载WPF BitmapImage
- 如何找出一个文件存在于c# / .NET?
- 为什么更快地检查字典是否包含键,而不是捕捉异常,以防它不?
- [DataContract]的命名空间
- string. isnullorempty (string) vs. string. isnullowhitespace (string)
- 完全外部连接
- 如何使用。net 4运行时运行PowerShell ?
- 在foreach循环中编辑字典值