我在ASP中遇到了一个小问题.NET 4,使用C#.我正在try 创建一个前端GUI,通过多个Web API发送和接收数据.拥有多个API的原因与我们的网络有关,它由几个安全区域组成.服务器位于不同的区域,一个简单的请求可能需要通过多达3个不同的API.
Specifically, I'm sending a JSON object from the GUI to the first API. The JSON object is supposed to be forwarded to the next API and the next - and from there a new JSON object is created and returned down the same path.
这条路本身运行良好.问题是,一旦JSON对象返回到GUI,就无法对其进行解析.我收到一个用引号括起来的JSON字符串,每个API对应一次.
字符串的开头可能如下所示:
Hey! I am a string, or a JSON object of sorts!
个
API之间的第一个 skip 给了我:
"Hey! I am a string, or a JSON object of sorts!"
After the next hop it looks like this:
"\"Hey! I am a string, or a JSON object of sorts!\""
当我的GUI掌握它时,我们就有了类似的东西:
"\"\\\"Hey! I am a string, or a JSON object of sorts!\\\"\""
This is where parsing fails, for obvious reasons. The JSON object itself is properly formatted, but all the quotes are causing problems for the JSON.net parser (all quotes inside the object are also wrapped multiple times).
到目前为止,我try 的是将请求作为application/json类型和text/Plain类型发送.两个人都做了同样的事情.API返回HttpResponseMessage,使用ReadAsStringAsync()读取.我还试图避免字符串读取,只是直接从HttpRequestMessage读取到HttpResponseMessage,并且只在GUI中执行ReadAsStringAsync(),但问题仍然存在.使用JSON.net Serialize()-method创建JSON字符串,并使用StringContent()将其放入HttpContent.这似乎正确地完成了工作.我相信引号是在API和GUI接收到HttpResponseMessage时生成的.
Any idea how I can send and receive the JSON string as a raw string, that is not treated in any way?
我可以通过在每个API上将对象解析为JToken或JObject,然后再次序列化来绕过这种行为.然而,这并不是一个好的解决方案——我宁愿API只是按照他们获得消息的方式转发消息,而不做任何处理.我研究了使用路由的转发,但是有很多授权的东西,这确实需要我使用API操作,而不是重定向路由.
To clarify (TLDR): The ideal solution would be for the API's to simply pass along the message without parsing or reading anything into it. As long as the source of the message is authorized, the request is encrypted and the requested URL is valid, the message itself doesn't matter much to each of the "proxy" API's.