使用Rest Service传递json c#

本文关键字:json 传递 Service Rest 使用 | 更新日期: 2023-09-27 17:58:37

我正在调用一个返回json 的REST服务

这就是我目前拥有的

HttpClient client = CreateClient(this.url, this.username, this.password);
string data_json = Newtonsoft.Json.JsonConvert.SerializeObject(Detail, Newtonsoft.Json.Formatting.Indented);
//Detail is a class with the json data
HttpResponseMessage response = client.GetAsync(uri).Result;
result = response.Content.ReadAsStringAsync().Result;

现在,我该如何使用data_json?我需要传递json来获得响应。

使用Rest Service传递json c#

您应该将其包含在您的帖子请求中:

StringContent stringContent = new StringContent(data_json, UnicodeEncoding.UTF8, "application/json");
var result = client.PostAsync(uri, stringContent).Result;