0

我有一个问题 - 我需要通过 POST 发送 json,但我的错误是“错误 (415) 无法处理消息,因为内容类型为 application/xml”,显然存在一些不匹配但我找不到它。这是我的 Web.config

    <configuration>
  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <behaviors>
      <serviceBehaviors>
        <behavior name="CustomBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="customBinding" closeTimeout="23:59:59" openTimeout="23:59:59" receiveTimeout="23:59:59" sendTimeout="23:59:59" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646">
          <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="ServiceContracts.Implementation.CR.CRService" behaviorConfiguration="CustomBehavior">
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint binding="wsHttpBinding" bindingConfiguration="customBinding" contract="ServiceContracts.CR.ICRService" />
        <endpoint binding="webHttpBinding" contract="ServiceContracts.CR.ICRService" behaviorConfiguration="restBehavior" address="rest" />
      </service>
      <service name="ServiceContracts.Implementation.SP1.SPOneService" behaviorConfiguration="CustomBehavior">
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint binding="wsHttpBinding" bindingConfiguration="customBinding" contract="ServiceContracts.SP1.ISPOneService" />
        <endpoint binding="webHttpBinding" contract="ServiceContracts.SP1.ISPOneService" behaviorConfiguration="restBehavior" address="rest"/>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="104857600" />
      </requestFiltering>
    </security>
  </system.webServer>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime executionTimeout="240" maxRequestLength="20480" />
  </system.web>

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
        <listeners>
          <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\Traces.svclog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>
4

2 回答 2

1

首先你需要一个代理阅读器来查看你实际发送的内容,我建议:Fiddler。然后,您可以将其与预期相匹配。

我还建议为您的 Web 服务使用ServiceStack,它们提供了出色的对象发送/接收处理。但如果你走得太远,你仍然可以使用 ServiceStack 的优秀对象到 JSON 转换器。ServiceStack 的巧妙之处在于没有配置文件。

于 2013-05-06T19:57:07.033 回答
0

PUT Http Verb出现同样的错误- 解决方案是添加以下请求标头:

接受:application/json
内容类型:application/json;字符集=utf-8

于 2013-06-03T11:50:49.337 回答