HTTP 415无法处理该消息,因为内容类型';application/json;charset=utf-8

本文关键字:application json utf-8 charset 类型 处理 消息 因为 HTTP | 更新日期: 2023-09-27 18:28:10

我们有一个在HTTPS上运行良好的Web服务,但在HTTPS上显示HTTP415错误。因此,在HTTP下,我们可以发出POST请求,发送和接收JSON而不会出现问题。当我们在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" />

HTTP 415无法处理该消息,因为内容类型';application/json;charset=utf-8

WCF可以有不同的HTTP或HTTPs端点。我认为这就是问题所在,所以我会把它作为一个"答案"(我希望它能帮助你):

您的端点name="ProductServiceEndPoint"address="在您的基地址处公开。OK

您的端点name="ProductServiceSecureEndPoint"address="ProductServiceSecure"bindingConfiguration="SecureBasicHttpBinding"它暴露在基"base_address]/ProductServiceSecurity"处。

所以这个端点:

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

这是不正确的,因为地址可能是"ProductServiceSecure"

basicHttpBinding不能与JSON一起使用。如果要使用JSON,请将basicHttpBinding(SOAP)更改为webHttpBinding。

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

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

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

configfile中添加服务标签name=";命名空间。"服务";然后在端点标签中

address="" behaviorConfiguration="web" binding="webHttpBinding"
     contract="namespace.IService"

和在IService接口

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