![](/img/trans.png)
[英]the content type 'application/json; charset=utf-8' was not the expected type '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字符串)。 我尝试将Binding更改为WebHttpBinding
,并添加了WebHttpBehavior
的端点行为,但这仅返回一个空对象。
我解决了 最初,我使用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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.