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'

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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

回答1:

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"



回答2:

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



回答3:

For this problem solution is that, In your request/response model there is some class who have not by default constructor which is parameterless.



回答4:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!