![](/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.