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

19

我正在尝试使用独立应用程序来消费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}

你的两个端点都出现了错误吗?另外,请发布异常堆栈。 - Dhawalk
检查异常内容。查看是否有“InnerException”或“Details”属性中的有用信息。 - Federico Berasategui
请查看此问题:https://dev59.com/52435IYBdhLWcg3wtSUV - Dhawalk
谢谢,但我需要知道如何将内容类型更改为正确的类型。 - level_zebra
似乎这个 WebService 是一个非 .NET 的 Web 服务,而你正在尝试将其与 WCF 绑定关联。为了使用它,只需删除所有 web.config 条目,然后在 VS.NET IDE 中使用“添加服务引用”选项,检查是否可以使用该服务。我猜你需要知道可以针对它使用的确切绑定,或者定义一个具有适当设置的自定义绑定。 - Rajesh
8个回答

18

你的 WCF 服务可能返回的是 HTML 内容。在这种情况下,你需要在服务端设置一个绑定以返回 XML 内容。但这种情况不太可能发生:如果确实是这种情况,请告诉我,我会提供更多详细信息。

更有可能的原因是你的服务抛出了错误,导致返回了 HTML 错误页面。如果你想了解详情,可以查看此博客文章

简而言之: 错误页面有几个可能的配置方式。如果你使用 IIS 进行托管,你需要从 WCF 服务的 web.config 文件中删除 <httpErrors> 部分。如果不是这种情况,请提供你的服务托管场景的详细信息,我可以提供相应的修改建议。

编辑:

看到你的编辑后,你可以看到返回的完整错误信息。Apache 不知道你要调用哪个服务,所以会报错。一旦你拥有了正确的端点,服务就能正常工作 - 目前你指向的位置是错误的。不幸的是,根据现有的信息,我无法确定正确的位置是什么,但你的操作(目前为 null!)或 URL 可能不正确。


这个 Web 服务是客户的,除了我被提供的 URL 来消费之外,我没有访问权限。 - level_zebra
嗨,我刚刚编辑了我的回复,看到了你放进去的堆栈跟踪。看起来你需要更改你的端点地址。 - Steve Westbrook
不,你在浏览器中输入的内容和WCF发送的内容可能不匹配。尝试使用自动生成的客户端代理并查看它使用的URL。 - Steve Westbrook
1
嗨,我用Fiddler检查了一下应用程序,它发送的是相同的URL,只是末尾没有"?wsdl"。如果需要,我可以发布项目代码。 - level_zebra
2
“?wsdl” 很重要 - 这意味着浏览器仅获取服务的“Web服务描述语言”,而不是调用该服务。如果您学会阅读 WSDL,就可以了解要发送的操作。将该操作添加到客户端的端点地址中,您应该就可以更轻松地使用该服务了。 - Steve Westbrook
显示剩余2条评论

6
当我遇到这个错误时,我花了几个小时来寻找解决方法。我的问题是,在保存文件时,我不小心按下了“G”键,导致web.config文件中有一个单独的字符未被正确格式化,因此web.config无法解释这些数据。希望这可以帮助你。

3

提前说明一下...
如果您正在使用 SoapUI模拟服务(作为服务器),并从 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 中编辑您的模拟响应并添加标头:

enter image description here

在我的情况下,这解决了问题。


我的问题完全解决了。非常感谢您的解决方案 :) - paulsm4

2
在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的不同版本,也会生成此错误。

我只在一个函数中使用WebImage来缩小上传图像的大小,但是却遇到了text/html异常。 - user3217843

1
我们使用IIS的Url Rewrite扩展将所有HTTP请求重定向到HTTPS。当尝试在http://...地址上调用未使用传输安全的服务时,会出现此错误。
因此,值得检查一下是否可以通过浏览器访问服务的http和https地址,并且它不会自动以303状态代码转发您。

0
在我的情况下,这只是web.config中的一个错误。
我有:
<endpoint address="http://localhost/WebService/WebOnlineService.asmx" 

本应该是:

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

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

0

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

在浏览器中打开您的 URL,例如:

http://localhost:61277/Email.svc

检查是否出现了500错误。

HTTP Error 500.19 - Internal Server Error

请查找以下部分中的错误:

配置错误

配置文件


0

尝试在IIS中浏览WCF,看看它是否正常运行。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接