内容类型application/json;响应消息的 charset=utf-8 与绑定的内容类型不匹配 (text/xml; charset=utf-8)
The content type application/json; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8)
我在尝试从 Web 服务接收数据时收到此错误消息。
这是我的代码:
ChannelFactory<IInterface> factory = new ChannelFactory<IInterface>(new BasicHttpBinding(), new EndpointAddress("http://example.net/MyService.svc/Test"));
var client = factory.CreateChannel();
MyObj x = client.Test();
虽然我收到错误,但我可以在错误消息中看到响应(JSON 字符串)。我尝试将绑定更改为 WebHttpBinding
,并添加了 WebHttpBehavior
的端点行为,但这只是 returns 一个空对象。
我解决了。最初我在使用 WebHttpBinding 时是正确的,但是对于端点行为我需要稍微修改它。这是工作代码:
ChannelFactory<IInterface> factory = new ChannelFactory<IInterface>(new WebHttpBinding(), new EndpointAddress("http://example.net/MyService.svc/Test"));
WebHttpBehavior behavior = new WebHttpBehavior()
{
DefaultOutgoingResponseFormat = WebMessageFormat.Json,
DefaultBodyStyle = WebMessageBodyStyle.Wrapped,
HelpEnabled = true,
DefaultOutgoingRequestFormat = WebMessageFormat.Json
};
factory.Endpoint.Behaviors.Add(behavior);
var client = factory.CreateChannel();
MyObj x = client.Test();
我在尝试从 Web 服务接收数据时收到此错误消息。
这是我的代码:
ChannelFactory<IInterface> factory = new ChannelFactory<IInterface>(new BasicHttpBinding(), new EndpointAddress("http://example.net/MyService.svc/Test"));
var client = factory.CreateChannel();
MyObj x = client.Test();
虽然我收到错误,但我可以在错误消息中看到响应(JSON 字符串)。我尝试将绑定更改为 WebHttpBinding
,并添加了 WebHttpBehavior
的端点行为,但这只是 returns 一个空对象。
我解决了。最初我在使用 WebHttpBinding 时是正确的,但是对于端点行为我需要稍微修改它。这是工作代码:
ChannelFactory<IInterface> factory = new ChannelFactory<IInterface>(new WebHttpBinding(), new EndpointAddress("http://example.net/MyService.svc/Test"));
WebHttpBehavior behavior = new WebHttpBehavior()
{
DefaultOutgoingResponseFormat = WebMessageFormat.Json,
DefaultBodyStyle = WebMessageBodyStyle.Wrapped,
HelpEnabled = true,
DefaultOutgoingRequestFormat = WebMessageFormat.Json
};
factory.Endpoint.Behaviors.Add(behavior);
var client = factory.CreateChannel();
MyObj x = client.Test();