POST操作的JSONObject随着HttpClient的从新REST API(preVIEW第4版) [英] POSTing JsonObject With HttpClient From New REST API (preview release 4)

查看:159
本文介绍了POST操作的JSONObject随着HttpClient的从新REST API(preVIEW第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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆