0

我正在尝试将一些 json 发布到 jboss 服务。使用restSharp ..我的代码如下。

        RestClient client = new RestClient(baseURL);
        RestRequest authenticationrequest = new RestRequest();
        authenticationrequest.RequestFormat = DataFormat.Json;
        authenticationrequest.Method = Method.POST;
        authenticationrequest.AddParameter("text/json",                  authenticationrequest.JsonSerializer.Serialize(prequestObj), ParameterType.RequestBody);

也试过这个

        RestClient client = new RestClient(baseURL);
        RestRequest authenticationrequest = new RestRequest();
        authenticationrequest.RequestFormat = DataFormat.Json;
        authenticationrequest.Method = Method.POST;                           authenticationrequest.AddBody(authenticationrequest.JsonSerializer.Serialize(prequestObj));

但在这两种情况下,我的服务器都给我错误,即 json 格式不正确

4

2 回答 2

0

尝试使用 JsonHelper 准备您的 json,如下所示

 string jsonToSend = JsonHelper.ToJson(prequestObj);

接着

authenticationrequest.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody);
于 2013-04-23T11:12:01.350 回答
0

我发现出了什么问题...

我在 Windows Metro Style 中使用 RestSharp,所以下载了源代码并进行了一些修改...以便在函数 PutPostInternalAsync 中进行修改我刚刚添加了这些修改

 httpContent = new StringContent(Parameters[0].Value.ToString(), Encoding.UTF8, "application/json");

它解决了问题....

Parameters[0].Value.ToString() 代替这个,你可以编写一个可以返回序列化 json 对象的方法。(作为字符串)。

于 2013-04-23T16:32:34.493 回答