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

标签 c# wcf

我们有一个在 HTTPS 上运行良好但在 HTTPS 上显示 HTTP 415 错误的网络服务。因此,在 HTTP 下,我们可以毫无问题地发送和接收 JSON 的 POST 请求。当我们在 HTTPS 下尝试相同的操作时,我们收到错误消息,即服务需要 text/xml 而不是 application/json。有什么关于去哪里看的建议吗?

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

更新了绑定(bind)和行为

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

最佳答案

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

您的端点名称="ProductServiceEndPoint"address="" 它暴露在您的基地址。好的

您的端点名称="ProductServiceSecureEndPoint"address="ProductServiceSecure"bindingConfiguration="SecureBasicHttpBinding"它暴露在基础“base_address]/ProductServiceSecure”。

所以这个端点:

  • 端点名称="DataServiceSecureEndPoint"address=""binding="basicHttpBinding"bindingConfiguration="SecureBasicHttpBinding"

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

关于c# - HTTP 415 无法处理消息,因为内容类型 'application/json; charset=utf-8' 不是预期的类型 'text/xml; charset=utf-8',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26625191/

相关文章:

c# - 希伯来语/犹太语日期转换

c# - 是否应该重构此 C# 代码以改用 Lazy<T> 类?

c# - 垃圾收集器是否清除订阅事件的对象?

c# - ReportingService2005 通过 WCF - 与模拟一起工作,但不与用户名/密码一起工作

c# - AddUrlSegment 在 RestSharp 中抛出 NullReferenceException

c# - 在 C# : Add Quotes around string in a comma delimited list of strings 中

c# - 在 Windows Phone Mango 中启用 WCF 调用

wcf - NetTcpBinding.MaxConnections 是否限制到端点的并发连接数或......?

asp.net - WCF服务支持文件jsdebug加载失败

wcf - WCF中的HttpContext