HTTP 415无法处理消息,因为内容类型“application/json; charset=utf-8”不是预期的类型“text/xml; charset=utf-8”。

11
我们有一个 Web 服务,在 HTTPS 上运行良好,但在 HTTP 上显示 HTTP 415 错误。因此,在 HTTP 下,我们可以发送和接收 JSON 的 POST 请求而没有问题。当我们尝试在 HTTPS 下执行相同的操作时,我们得到了错误,该服务期望 text/xml 而不是 application/json。有任何建议在哪里寻找解决方案吗?
如果有影响的话,服务器使用自签名证书。
附带绑定和行为更新。
 <!-- Wcf Services Setting -->
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WsHttpBinding" maxReceivedMessageSize="1048576">
          <readerQuotas maxArrayLength="1048576" />
        </binding>
        <binding name="SecureWsHttpBinding" maxReceivedMessageSize="1048576">
          <readerQuotas maxArrayLength="1048576" />
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </wsHttpBinding>
      <webHttpBinding>
        <binding name="WebHttpBinding" maxReceivedMessageSize="1048576">
          <readerQuotas maxArrayLength="1048576" />
        </binding>
        <binding name="SecureWebHttpBinding" maxReceivedMessageSize="1048576">
          <readerQuotas maxArrayLength="1048576" />
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
          <binding name="webBinding">
              <security mode="Transport">
              </security>
          </binding>
      </webHttpBinding>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IMainService" maxReceivedMessageSize="1048576"></binding>
        <binding name="BasicHttpBinding" maxReceivedMessageSize="1048576">
          <readerQuotas maxArrayLength="1048576" />
            <security mode="None">
                <transport clientCredentialType="None" />
            </security>
        </binding>
        <binding name="SecureBasicHttpBinding" maxReceivedMessageSize="1048576">
          <readerQuotas maxArrayLength="1048576" />
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehavior">
          <webHttp DefaultOutgoingResponseFormat="json" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="DvaMfs.WcfService">
        <useRequestHeadersForMetadataAddress>
                    <defaultPorts>
                        <add scheme="https" port="443" />
                    </defaultPorts>
                </useRequestHeadersForMetadataAddress>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

服务的外观如下所示

<service name="DvaMfs.WcfService.ProductService" behaviorConfiguration="DvaMfs.WcfService">
    <endpoint name="ProductServiceEndPoint" address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="DvaMfs.WcfService.IProductService" />
    <endpoint name="ProductServiceAjaxEndPoint" address="ajax" binding="webHttpBinding" bindingConfiguration="WebHttpBinding" behaviorConfiguration="AjaxBehavior" contract="DvaMfs.WcfService.IProductService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <endpoint name="ProductServiceSecureEndPoint" address="ProductServiceSecure" binding="basicHttpBinding" bindingConfiguration="SecureBasicHttpBinding" contract="DvaMfs.WcfService.IProductService" />
    <endpoint name="ProductServiceAjaxSecureEndPoint" address="ProductServiceSecureajax" binding="webHttpBinding" bindingConfiguration="SecureWebHttpBinding" behaviorConfiguration="AjaxBehavior" contract="DvaMfs.WcfService.IProductService" />
  </service>

更新 2 以下是其中一个请求终端失败:

<endpoint name="DataServiceSecureEndPoint" address="" binding="basicHttpBinding"
bindingConfiguration="SecureBasicHttpBinding" contract="DvaMfs.WcfService.IDataService" />

顺便说一句,我在 Stack Overflow 上尝试了所有关于这个相同错误的问题的建议,但都没有成功。 - momo
@CapitanCavernícola 已更新问题,并附上了部分配置文件。 - momo
你是将失败的终端点作为SOAP服务还是REST服务进行调用?basicHttpBinding是SOAP绑定,通常SOAP返回XML而不是JSON,但根据错误信息,似乎该服务正在返回JSON。 - Tim
1
也许你可以“注释掉”除了'ProductServiceAjaxSecureEndPoint'之外的所有端点,以确保你正在使用它。 - Morcilla de Arroz
我正在从网页以ajax POST的方式调用端点,而从移动应用程序中则以普通POST的方式调用,我们发送JSON并期望JSON。服务器说它期望XML。有趣的是,相同的代码在HTTP上运行良好,这个错误只发生在HTTPS上 :S - momo
显示剩余3条评论
5个回答

9
WCF可以有不同的HTTP或HTTPS端点。 我认为这是问题所在,所以我会将其作为“答案”(希望能帮助您):

您的endpoint name="ProductServiceEndPoint" address=""暴露在基本地址上。 好

您的endpoint name="ProductServiceSecureEndPoint" address="ProductServiceSecure" bindingConfiguration="SecureBasicHttpBinding"暴露在基础“base_address]/ProductServiceSecure”。

因此,此端点:

  • endpoint name="DataServiceSecureEndPoint" address="" binding="basicHttpBinding" bindingConfiguration="SecureBasicHttpBinding"

是不正确的,因为地址可能是“ProductServiceSecure”。


我理解了,我接受它,并希望它能帮助未来有类似问题的人。问候;-) - momo

2

basicHttpBinding无法与JSON一起使用。如果您想使用JSON,请将basicHttpBinding(SOAP)更改为webHttpBinding(REST)。


那么您如何解释相同的代码在HTTP上使用JSON正常工作,而只有在使用HTTPS进行POST时才会出现此问题。我不明白为什么。 - momo
1
我认为您正在检查错误的端点或调用服务时未请求json,因为basicHttpBinding根本不支持json。 - Peter Kiss
1
不能完全相同的代码,我猜你无论如何都要设置代理。为了进行更多测试:你可以尝试使用'only' <webHttp/> 代替 '<webHttp DefaultOutgoingResponseFormat="json" />',并注释掉你没有在HTTPS中使用的端点。 - Morcilla de Arroz

1
针对这个问题的解决方案是,在您的请求/响应模型中,有些类没有默认构造函数,即无参数构造函数。

0
configfile 中添加服务标签 name="namespace.Service",然后在 endpoint 标签中。
address="" behaviorConfiguration="web" binding="webHttpBinding"
     contract="namespace.IService"

并且在 IService 接口中

[WebInvoke(Method = "POST", UriTemplate = "functionname", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]

0

最后,我们的后端开发人员更改了终端地址字段,并将其路由到特定路径(而不是地址=“”)以测试其是否正常工作。根据他所说,HTTP和HTTPS终端点尝试使用相同的地址,但这并不起作用。因此,他最终注释了HTTP终端点,并为HTTPS终端点设置了地址。

我不知道这是否有意义,因为我对WCF一无所知。对于我来说,对Apache服务器有一些了解,似乎应该能够指定终端点,并且不应基于/链接到用于连接的协议。


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