简体   繁体   中英

HTTP 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

We have a webservice that works fine on HTTPS but shows the HTTP 415 error on HTTPS. So, under HTTP, we can make a POST request sending and receiving JSON without issue. When we try the same under HTTPS we got the error that the service is expecting text/xml insteas of application/json. Any suggestion on where to look?

The server is using a self signed certificate if that matters.

Updated with bindings and behaviors

 <!-- 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>

services look like this

<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>

Update 2 This is one of the endpoints failing:

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

WCF can have different endpoints for HTTP or HTTPs. I think this is the problem, so I will put it as an "answer" (I hope it helps you):

Your endpoint name="ProductServiceEndPoint" address="" it's exposed at your base address. OK

Your endpoint name="ProductServiceSecureEndPoint" address="ProductServiceSecure" bindingConfiguration="SecureBasicHttpBinding" it's exposed at base "base_address]/ProductServiceSecure".

So this endpoint:

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

It's incorrect, because the address may be "ProductServiceSecure"

The basicHttpBinding can not work with JSON. Change the basicHttpBinding (SOAP) to webHttpBinding (REST) if you want to use JSON.

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

At the end our back-end developer changed the endpoint address field and routed it to specific paths (instead of address="") to test if it was working and it was. Apparently, according to what he said, the HTTP and HTTPS endpoints were trying to use the same address and that wasn't working. So he finally commented the HTTP endpoints and set the address for the HTTPS ones.

I don't know if that make much sense since I have no idea about WCF. To me, having some knowledge on Apache servers, it seems that you should be able to specify an endpoint and it shouldn't be based on/linked to the protocol used for connecting to it.

In configfile add in service tag name="namespace.Service" then in endpoint tag

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

and in IService interface

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' Cannot process the message because the content type 'application / xml' was not the expected type 'application / soap + 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) 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), WCF The content type text/xml; charset=“utf-8” of the response message does not match the content type of the binding (text/xml; charset=utf-8) 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) WCF ERROR: (415) content type 'application/x-www-form-urlencoded' was not the expected type 'application/soap+xml; charset=utf-8' WCF Membership Provider throws error: content type 'application/json; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8' WCF SOAP service cannot process the message because it sends multipart message and expects 'text/xml; charset=utf-8'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM