繁体   English   中英

服务器无法识别json,使用restSharp发送请求

[英]Server is not Recognizing the json, sending request using restSharp

我正在尝试将一些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的格式不正确

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

 string jsonToSend = JsonHelper.ToJson(prequestObj);

接着

authenticationrequest.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody);

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

我正在Windows Metro Style中使用RestSharp,因此下载了源代码并进行了一些修改...,以便对功能PutPostInternalAsync进行修改,我刚刚添加了此修改

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

它解决了问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM