16

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

8 回答 8

18

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

更可能的原因是您的服务抛出了一个错误,即返回一个 HTML 错误页面。如果您想了解详细信息,可以查看此博客文章。

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

编辑:

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

于 2013-03-18T16:05:25.057 回答
6

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

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

希望这可以帮助。

于 2014-10-29T15:52:32.707 回答
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 编辑您的 Mock Response 并添加一个 Header 到它: 在此处输入图像描述

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

于 2016-09-30T08:30:50.893 回答
1

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

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

于 2015-11-06T14:50:26.387 回答
1

在 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 时,甚至会生成此错误。

于 2016-11-07T07:56:49.920 回答
0

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

我有:

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

它应该是:

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

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

于 2016-06-06T23:58:56.873 回答
0

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

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

http://localhost:61277/Email.svc

检查您是否有 500 错误。

HTTP Error 500.19 - Internal Server Error

在这些部分中查找错误:

配置错误

配置文件

于 2016-06-27T17:51:38.400 回答
0

尝试在 IIS 中浏览 WCF,看看它是否还活着并正常工作,

就我而言,这是因为 WCF 的物理路径被误导了。

于 2017-07-31T06:50:02.753 回答