使用WCF通过post发送xml,但收到错误415

时间:2014-03-01 22:14:01

标签: wcf

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

  

远程服务器返回错误:(415)无法处理消息   因为内容类型'application / xml'不是预期的类型   “文本/ XML;字符集= 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的引用帖子相同。请帮我。提前致谢!!

2 个答案:

答案 0 :(得分:1)

要使用 休息 ,您必须拥有 webHttpBinding 。尝试改变它。

答案 1 :(得分:1)

将您的绑定更改为 webHttpBinding

binding="basicHttpBinding"

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

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