使用传递json c#的Rest Service [重复]

Posted

技术标签:

【中文标题】使用传递json c#的Rest Service [重复]【英文标题】:Using Rest Service passing json c# [duplicate] 【发布时间】:2016-11-30 11:48:42 【问题描述】:

我正在调用一个返回 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 来获得响应。

【问题讨论】:

在帖子正文中传递? 【参考方案1】:

您应该将其包含在您的发布请求中:

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

【讨论】:

以上是关于使用传递json c#的Rest Service [重复]的主要内容,如果未能解决你的问题,请参考以下文章