c# http post json
时间: 2024-01-30 11:02:53 浏览: 137
以下是使用C#进行HTTP POST请求发送JSON数据的示例代码:
```csharp
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
public class Program
{
public static async Task Main()
{
string url = "http://192.168.001.110:8086/api/v1/settimecfg";
string json = "{\"day\":8,\"hour\":15,\"min\":42,\"month\":5,\"sec\":15,\"week\":1,\"year\":2023}";
using (HttpClient client = new HttpClient())
{
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
Console.WriteLine(result);
}
else
{
Console.WriteLine("HTTP POST request failed with status code: " + response.StatusCode);
}
}
}
}
```
请注意,上述代码中的`url`变量是目标URL,`json`变量是要发送的JSON数据。使用`HttpClient`类进行POST请求,并将JSON数据作为请求的内容发送。如果请求成功,将打印出响应的结果;如果请求失败,将打印出错误信息。
阅读全文