POST操作的JSONObject随着HttpClient的从新REST API(preVIEW第4版) [英] POSTing JsonObject With HttpClient From New REST API (preview release 4)
问题描述
我想发布一个的JSONObject
使用的HttpClient
在新的WCF的Web API(preVIEW 4)。我不太知道如何去这件事,不能找到样本code的方式很多。
I'm trying to POST a JsonObject
using HttpClient
from the new WCF Web API (preview 4). I'm not quite sure how to go about this and can't find much in the way of sample code.
下面是我到目前为止有:
Here's what I have so far:
var myObject = (dynamic)new JsonObject();
myObject.Data = "some data";
myObject.Data2 = "some more data";
HttpClient httpClient = new HttpClient("myurl");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.Post("", ???);
我想我需要投我的的JSONObject
为 StreamContent
,但我发现挂在那一步。
I think I need to cast my JsonObject
as a StreamContent
but I'm getting hung up on that step.
我使用WCF的Web API preVIEW 4作为 HTTP发现://wcf.$c$cplex.com
I'm using WCF Web API Preview 4 as found at http://wcf.codeplex.com
推荐答案
最简单的方法是使用的StringContent
,你JSON的JSON重新presentation对象。
The easiest way is to use a StringContent
, with the JSON representation of your JSON object.
httpClient.Post(
"",
new StringContent(
myObject.ToString(),
Encoding.UTF8,
"application/json"));
这篇关于POST操作的JSONObject随着HttpClient的从新REST API(preVIEW第4版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!