响应消息的内容类型application/xml;charset=utf-8 与绑定的内容类型不匹配(text/xml;charset=utf-8)

Posted

技术标签:

【中文标题】响应消息的内容类型application/xml;charset=utf-8 与绑定的内容类型不匹配(text/xml;charset=utf-8)【英文标题】:The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) 【发布时间】:2013-03-07 00:59:37 【问题描述】:

我尝试使用独立应用程序使用 WCF Web 服务。我可以使用 Internet Explorer 查看此服务,也可以在 Visual Studio 服务引用中查看。

这是我遇到的错误

The content type text/html; charset=UTF-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8).

如何更改它以使用正确的内容类型?

这是我的配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="G2WebServiceSoap11Binding" />
        </basicHttpBinding>
        <customBinding>
            <binding name="G2WebServiceSoap12Binding">
                <textMessageEncoding messageVersion="Soap12" />
                <httpTransport />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint address="http://XXX.XX.XX.XX:XX/janus/services/G2WebService.G2WebServiceHttpSoap11Endpoint/"
            binding="basicHttpBinding" bindingConfiguration="G2WebServiceSoap11Binding"
            contract="G2ServiceReference.G2WebServicePortType"
            name="G2WebServiceHttpSoap11Endpoint" />
        <endpoint address="http://XXX.XX.XX.XX:XX/janus/services/G2WebService.G2WebServiceHttpSoap12Endpoint/"
            binding="customBinding" bindingConfiguration="G2WebServiceSoap12Binding"
            contract="G2ServiceReference.G2WebServicePortType"
            name="G2WebServiceHttpSoap12Endpoint" />
    </client>
</system.serviceModel>

这是堆栈

System.ServiceModel.ProtocolException: The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. 
The first 1024 bytes of the response were: '<Exception>org.apache.axis2.AxisFault: The endpoint reference (EPR) for the Operation not found is /janus/services/G2WebService and the WSA Action = null&#xd;
    at org.apache.axis2.engine.DispatchPhase.checkPostConditions(DispatchPhase.java:89)&#xd;
    at org.apache.axis2.engine.Phase.invoke(Phase.java:333)&#xd;
    at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:264)&#xd;
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:163)&#xd;
    at org.apache.axis2.transport.http.util.RESTUtil.invokeAxisEngine(RESTUtil.java:136)&#xd;
    at org.apache.axis2.transport.http.util.RESTUtil.processURLRequest(RESTUtil.java:130)&#xd;
    at org.apache.axis2.transport.http.AxisServlet$RestRequestProcessor.processURLRequest(AxisServlet.java:829)&#xd;
    at org.apache.axis2.transport.http.AxisServlet.doGet(AxisServlet.java:255)&#xd;
    at com.rm.janus.webservice.GroupCallServlet.doGet(GroupCallServlet.java:33)&#xd;
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)&#xd;
    at javax.servlet.http.HttpSer'. ---> System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory`1 factory, WebException responseException, ChannelBinding channelBinding)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at InetgrisG2TestApp.G2ServiceReference.G2WebServicePortType.getStudentData(getStudentDataRequest request)
   at InetgrisG2TestApp.G2ServiceReference.G2WebServicePortTypeClient.G2TestApp.G2ServiceReference.G2WebServicePortType.getStudentData(getStudentDataRequest request) in c:\Users\s\Documents\Visual Studio 2012\Projects\G2TestApp\InetgrisG2TestApp\Service References\G2ServiceReference\Reference.cs:line 2981
   at InetgrisG2TestApp.G2ServiceReference.G2WebServicePortTypeClient.getStudentData(ServiceRequest serviceReq) in c:\Users\s\Documents\Visual Studio 2012\Projects\G2TestApp\G2TestApp\Service References\G2ServiceReference\Reference.cs:line 2987
   at InetgrisG2TestApp.Program.Main(String[] args) in c:\Users\s\Documents\Visual Studio 2012\Projects\G2TestApp\G2TestApp\Program.cs:line 57

【问题讨论】:

两个端点都有错误吗?另外,请发布异常堆栈 查看异常内容。看看你是否有InnerException,或者Details 属性中有用的东西。 检查这个问题***.com/questions/5263150/… 谢谢,但我需要知道如何将内容类型更改为正确的 似乎 WebService 是一个非 .NET Web 服务,而您正试图将它与 WCF 绑定相关联。为了使用它,只需删除所有 web.config 条目,然后使用 VS.NET IDE 中的添加服务引用选项并检查您是否可以使用该服务。我想您需要知道可以对它使用的确切绑定,或者使用适当的设置定义自定义绑定。 【参考方案1】:

您的 WCF 服务可能正在返回 HTML。在这种情况下,您需要在服务端设置一个绑定以返回 XML。但是,这不太可能:如果是,请告诉我,我会进行更详细的编辑。

更可能的原因是您的服务抛出错误,即返回 HTML 错误页面。想了解详情可以关注this blog post。

tl;博士: 错误页面有几种可能的配置。如果您在 IIS 上托管,则需要从 WCF 服务的 web.config 文件中删除 &lt;httpErrors&gt; 部分。如果没有,请提供您的服务托管方案的详细信息,我可以进行修改以匹配它们。

编辑:

查看您的编辑后,您可以看到返回的完整错误。 Apache 无法判断您要调用哪个服务,因此会抛出错误。一旦您拥有正确的端点,该服务就会正常工作 - 您被指向错误的位置。不幸的是,我无法从可用的信息中判断 正确 位置是什么,但是您的操作(当前为 null!)或 URL 不正确。

【讨论】:

WEB服务是客户,除了给我消费的url之外,我无权访问 嗨,刚刚编辑了我的回复,看到了你输入的堆栈跟踪。看起来你需要更改你的端点地址。 您好,感谢您的回复。当我将它粘贴到浏览器中时,该 url 解析正常。这会消除 url 的问题吗? 嗨,我和 fiddler 一起检查了应用程序正在发送相同的 url,尽管最后没有 ?wsdl。如果有帮助,我可以发布项目的代码 ?wsdl 很重要——这意味着浏览器只是获取服务的“Web 服务描述语言”,而不是调用服务。如果您学会阅读 WSDL,它将告诉您要发送的操作。将该操作添加到客户端的端点地址,您应该对服务有更多的运气。【参考方案2】:

当我遇到这个错误时,我花了几个小时试图找到解决方案。

我的问题是,当我去保存文件时,我不小心在 web.config 中按下了击键“G”。我有一个散乱的角色坐在外面,所以 web.config 不知道如何解释格式不正确的数据。

希望这会有所帮助。

【讨论】:

【参考方案3】:

以防万一... 如果您使用 SoapUI Mock Service(作为服务器),从 C# WCF 调用它:

WCF --> SoapUI MockService

在这种情况下,您会遇到同样的错误:

The content type text/html; charset=UTF-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8).

在 SoapUI 中编辑您的 Mock Response 并为其添加一个 Header:

在我的场景中,这可以解决问题。

【讨论】:

正是我的问题。一百万感谢您的解决方案:)【参考方案4】:

我们使用 IIS 的 Url Rewrite 扩展将所有 HTTP 请求重定向到 HTTPS。尝试在 http://... 地址上调用未使用传输安全性的服务时,出现了此错误。

因此,可能值得检查您是否可以通过浏览器同时访问服务的 http 和 https 地址,并且它不会自动向您转发 303 状态代码。

【讨论】:

【参考方案5】:

在 WCF 服务项目中,此问题可能是由于引用 System.Web.Mvc.dll 的不同版本或可能是任何其他 DLL 的不同版本问题。所以这可能是DLL不同版本的兼容性问题

当我使用时

System.Web.Mvc.dll 版本 5.2.2.0 -> 它会抛出 错误 内容类型 text/html;响应消息的charset=utf-8

但是当我使用时

System.Web.Mvc.dll 版本 4.0.0.0 或更低版本 -> 在我的项目中工作正常并且没有错误。

我不知道这个不同版本的 DLL 问题的原因,但是当我更改与您的 WCF 项目兼容的 DLL 版本时,它可以正常工作。

当您在 WCF 项目中添加其他项目的引用并且此引用项目具有不同版本的 System.Web.Mvc DLL 或可能是任何其他 DLL 时,甚至会生成此错误。

【讨论】:

我只为一个函数获取 text/html 异常,在该函数中我使用 WebImage 减小上传图像的大小。【参考方案6】:

就我而言,这只是 web.config 中的一个错误。

我有:

<endpoint address="http://localhost/WebService/WebOnlineService.asmx" 

应该是:

<endpoint address="http://localhost:10593/WebService/WebOnlineService.asmx"

地址中缺少端口号 (:10593)。

【讨论】:

【参考方案7】:

这可能是 web.config 文件中的错误。

在浏览器中打开你的网址,例如:

http://localhost:61277/Email.svc

检查您是否有 500 错误。

HTTP Error 500.19 - Internal Server Error

在这些部分中查找错误:

配置错误

配置文件

【讨论】:

【参考方案8】:

尝试在 IIS 中浏览 WCF,看看它是否处于活动状态并正常工作,

在我的情况下,这是因为 WCF 的物理路径被误导了。

【讨论】:

以上是关于响应消息的内容类型application/xml;charset=utf-8 与绑定的内容类型不匹配(text/xml;charset=utf-8)的主要内容,如果未能解决你的问题,请参考以下文章

Asp.Net Web API 错误:“ObjectContent`1”类型无法序列化内容类型“application/xml”的响应正文;字符集=utf-8'

“ObjectContent`1”类型未能序列化内容类型“application/xml; charset=utf-8”的响应正文。

C# WebApi Xml序列化问题解决方法:“ObjectContent`1”类型未能序列化内容类型“application/xml;charset=utf-8"的响应正文。...

webapi “ObjectContent`1”类型未能序列化内容类型“application/xml; charset=utf-8”的响应正文。

无法处理该消息,因为内容类型“应用程序/xml”不是预期的类型“应用程序/soap + xml”;字符集 = utf-8 '

Asp.Net Web API错误:'ObjectContent`1'类型无法序列化内容类型'application / xml的响应主体;字符集= UTF-8'(代码