【问题标题】:Sending xml via post with WCF but getting error 415使用 WCF 通过 post 发送 xml 但出现错误 415
【发布时间】:2014-03-01 22:14:00
【问题描述】:

我根据 stackoverflow 的这篇文章构建了一个 wcf 服务:How to send xml via post with wcf 但我收到了这个错误:

远程服务器返回错误:(415)无法处理消息 因为内容类型“application/xml”不是预期的类型 '文本/xml; charset=utf-8'..

我认为我的 Web.config 有问题,我将其发布在这里:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="BridgeService.BridgeData" behaviorConfiguration="RESTBehavior">
<endpoint address="" binding="basicHttpBinding"   contract="BridgeService.IBridgeData" behaviorConfiguration="MyEndpointBehavior">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:10912/Vishi/BridgeService/" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="RESTBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="MyEndpointBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

这是界面:

    [ServiceContract]
public interface IBridgeData
{
    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "InsertData",
    RequestFormat = WebMessageFormat.Xml,
    BodyStyle = WebMessageBodyStyle.Bare)]
    string InsertData(transport_xml transport_xml);
}

并且调用与 Stackoverflow 的引用帖子相同。请帮我。提前致谢!!

【问题讨论】:

    标签: wcf


    【解决方案1】:

    要使用 Rest 行为,您必须具有 webHttpBinding。尝试改变它。

    【讨论】:

      【解决方案2】:

      将您的绑定更改为 webHttpBinding

      binding="basicHttpBinding"
      

      并确保在您提出请求时,不要忘记添加Content-Type like:

      Content-Type: application/xml;charset=utf-8
      

      【讨论】:

        猜你喜欢
        • 2018-06-13
        • 2019-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-21
        • 1970-01-01
        • 2014-06-10
        相关资源
        最近更新 更多