用REST协议调用http://172.28.206.142:9020/con/common_interface接口uploadDkRecord发送JSON格式报文,内容为"sn" : "xxx","machineId" : "xxx","result " : "OK",
时间: 2023-11-21 13:58:31 浏览: 37
你可以使用C#中的HttpClient类来发送REST请求。以下是一个示例代码,可以向指定的URL发送JSON格式的POST请求:
```csharp
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace RestExample
{
class Program
{
static async Task Main(string[] args)
{
var url = "http://172.28.206.142:9020/con/common_interface/uploadDkRecord";
var json = "{\"sn\":\"xxx\",\"machineId\":\"xxx\",\"result\":\"OK\"}";
using (var httpClient = new HttpClient())
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("请求成功!");
}
else
{
Console.WriteLine("请求失败!");
}
}
}
}
}
```
在上面的代码中,我们首先定义了要发送的URL和JSON格式的报文。然后,我们使用HttpClient类创建一个HTTP客户端,并使用PostAsync方法发送POST请求。在请求中,我们将JSON报文作为请求体发送,并设置Content-Type为application/json。最后,我们检查响应的状态码,以确定请求是否成功。