由于请求体中的反斜杠,Json请求失败?

时间:2015-09-07 11:42:47

标签: c# json serialization restsharp

我目前开始开发将现有应用移植到Windows商店应用程序。

我正在使用Restsharp.portable(FubarDevelopment)和JSON.Net进行序列化。

我尝试通过构建请求对象并将序列化对象添加为requestBody来执行登录。 但是,此请求始终以HttpStatusCode 500失败。当我看一下Fiddler时,我可以看到JSON主体仍然包含转义斜杠。 当我从Fiddler手动编辑请求时,删除那些反斜杠并执行它,请求得到HttpStatusCode 200处理。

有人知道它为什么要使用转义斜杠发送我的请求者吗?

这是Fiddler

中显示的原始内容
POST http://myhost/webmediation/service.php HTTP/1.1
Accept: application/json, text/json, text/x-json, text/javascript,application/xml, text/xml
Content-Length: 102
Accept-Encoding: identity
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
User-Agent: NativeHost
Host: 185.7.39.125
Connection: Keep-Alive
Pragma: no-cache
"{\"params\":\"p_strLogin\":\"flow@mcs\"},\"jsonrpc\":\"2.0\",\"method\":\"initLogin\",\"id\":\"1\"}"

这是我序列化对象并将其添加到请求

的代码
var requestJSON = JsonConvert.SerializeObject(jsonRequestObject, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

        var request = new RestRequest(resource, method);
        request.AddParameter("application/json; charset=utf-8", requestJSON, ParameterType.RequestBody);

解决: 正如Pranav Patel所述,该物体第二次被连载。将对象添加到请求时,Restsharp.portable实现执行JSON序列化。由于我在请求中添加了一个已经序列化的对象,因此它已被转义。

谢谢Pranav!

0 个答案:

没有答案
相关问题