我有两个应用程序。一个请求客户端应用程序是用ASP.NET Core2编写的(我们可以称其为“客户端”),一个响应/ API应用程序是用ASP.NET 4WebAPI编写的(我们可以称其为“服务器”)。前者向后者发出请求,而我希望从web API应用程序接收并随后解析XML。
客户端使用以下设置发出HTTP请求:
HttpClient httpClient = new HttpClient()
{
BaseAddress = new Uri(communicatorUri)
};
httpClient.DefaultRequestHeaders.Add("Accept", "application/xml");
HttpResponseMessage responseMessage = await this.HttpClient.PutAsync(
"api/customer",
new StringContent(customerDetails.ToString(), Encoding.UTF8, "application/json")
);
它通过JSON发送,并等待XML响应。服务器正在处理JSON,并且希望返回一个XML响应:
string response = "<note>Well formed XML string</note>";
return Ok(response);
此响应返回到客户端的位置:
string responseString = await responseMessage.Content.ReadAsStringAsync();
XDocument xml = XDocument.Parse(responseString);
但是,每次返回响应时,它看起来就像是使用\"
的双重编码。例如,它可能看起来像"\"<note attr=\\\"test\\\">Well formed XML string</note>"
,因此随后它显然不再是有效的XML,因此通过抛出异常导致解析失败。
我从哪里开始实际解决这个问题呢?我有点困惑,我不知道我是否需要改变我的请求者,或者我的响应,甚至两者都需要。
发布于 2018-08-10 09:16:38
更新服务器上的控制器:
public HttpResponseMessage Get()
{
//process the request
.........
string XML="<note><body>Message content</body></note>";
return new HttpResponseMessage()
{
Content = new StringContent(XML, Encoding.UTF8, "application/xml")
};
}
https://stackoverflow.com/questions/51777156
复制相似问题