HTTP 415无法处理消息,因为内容类型' application / json;字符集= UTF-8'不是预期的类型&text; / xml;字符集= UTF-8'

时间:2014-10-29 07:53:40

标签: c# wcf

我们有一个可以在HTTPS上正常工作的网络服务,但在HTTPS上显示HTTP 415错误。因此,在HTTP下,我们可以发出POST请求发送和接收JSON而不会出现问题。当我们在HTTPS下尝试相同时,我们得到了错误,即服务期望application / json的text / xml insteas。关于在哪里看的任何建议?

如果重要,服务器正在使用自签名证书。

更新了绑定和行为

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

4 个答案:

答案 0 :(得分:5)

WCF可以为HTTP或HTTP提供不同的端点。 我认为这是问题,所以我会把它作为一个&#34;答案&#34; (我希望它可以帮助你):

您的端点名称=&#34; ProductServiceEndPoint&#34;地址=&#34;&#34; 它暴露在你的基地址。行

您的端点名称=&#34; ProductServiceSecureEndPoint&#34;地址=&#34; ProductServiceSecure&#34; bindingConfiguration =&#34; SecureBasicHttpBinding&#34; 它暴露在base&#34; base_address] / ProductServiceSecure&#34;。

所以这个终点:

  • 端点名称=&#34; DataServiceSecureEndPoint&#34;地址=&#34;&#34;结合=&#34; basicHttpBinding的&#34;的 bindingConfiguration =&#34; SecureBasicHttpBinding&#34;

这是不正确的,因为地址可能是&#34; ProductServiceSecure&#34;     

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

最后,我们的后端开发人员更改了端点地址字段并将其路由到特定路径(而不是address =“”)以测试它是否正常工作。显然,根据他的说法,HTTP和HTTPS端点试图使用相同的地址,但是没有用。所以他最后评论了HTTP端点,并设置了HTTPS端点的地址。

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