【问题标题】:WCF REST adding data using POST or PUT 400 Bad RequestWCF REST 使用 POST 或 PUT 400 错误请求添加数据
【发布时间】:2010-12-10 17:44:31
【问题描述】:

你好 如何使用 wcf REST 架构添加数据。 我不想使用 channelfactory 来调用我的方法。 类似于用于 GET 的 webrequest 和 webresponse。类似于 ajax WebServiceProxy restInvoke 的东西 还是我总是必须使用 Webchannelfactory 实现

我通过以下方式收到 400 BAD 请求

将网址调暗为字符串 = "http://localhost:4475/Service.svc/Entity/Add" Dim req As WebRequest = WebRequest.Create(url) req.Method = "POST" req.ContentType = "应用程序/xml; charset=utf-8" 请求超时 = 30000 req.Headers.Add("SOAPAction", url)

    Dim xEle As XElement
    xEle = <Entity xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                 <Name>Entity1</Name>
             </Entity>

    Dim sXML As String = xEle .Value
    req.ContentLength = sXML.Length
    Dim sw As New System.IO.StreamWriter(req.GetRequestStream())
    sw.Write(sXML)
    sw.Close()

    Dim res as HttpWebResponse = req.GetResponse()

    Sercice Contract is as follows

   <OperationContract()> _
   <WebInvoke(Method:="PUT", UriTemplate:="Entity/Add")> _
   Function AddEntity(ByVal e1 As Entity)

     DataContract is as follows

    <Serializable()> _
    <DataContract()> _
    Public Class Entity
      private m_Name as String
     <DataMember()> _
      Public Property Name() As String
      Get
        Return m_Name
      End Get
      Set(ByVal value As String)
        m_Name = value
      End Set
      End Property
    End Class

谢谢

【问题讨论】:

    标签: wcf rest


    【解决方案1】:

    在 REST 中,您可以使用 HTTP POST(如果服务器分配资源名称)或 HTTP PUT(如果客户端分配资源名称)来创建资源(即添加数据)。您使用 PUT 更新资源,并使用 DELETE 删除它。只有 HTTP 方法发生了变化。

    Rob Bagby 在REST in WCF 上有一个包含 11 部分的系列。

    【讨论】:

      【解决方案2】:

      我怀疑您的 XML 中可能缺少某些名称空间,或者您没有按照 DataContractSerializer 的要求格式化 XML。

      尝试仅使用 DataContractSerializer 反序列化 Entity 类的实例,并查看 XML 应该是什么样子。

      【讨论】:

        猜你喜欢
        • 2012-01-20
        • 1970-01-01
        • 2020-05-09
        • 2011-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        相关资源
        最近更新 更多