WCF REST使用POST或PUT 400错误请求添加数据
问题描述:
HI 如何使用wcf rest架构添加数据。 我不想使用channelfactory来调用我的方法。 与用于GET的webrequest和webresponse类似。同样的事情也给Ajax WebServiceProxy restInvoke 还是我总是要使用Webchannelfactory实施WCF REST使用POST或PUT 400错误请求添加数据
我通过以下
昏暗的URL的String =“http://localhost:4475/Service.svc/Entity/Add” 昏暗REQ作为WebRequest的得到一个400错误的请求=“WebRequest.Create(url) req.Method =”POST“ req.ContentType =”application/xml; charset = utf-8“ req.Timeout = 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
感谢
答
在REST中,您创建一个资源(即,添加数据)或者通过(如果服务器分配资源名称)或HTTP PUT使用HTTP POST (如果客户端正在分配资源名称)。您使用PUT更新资源,并使用DELETE删除它。只有HTTP方法更改。
Rob Bagby有关于REST in WCF的11部分系列。
答
我怀疑你可能在你的XML中缺少一些命名空间,或者你不像DataContractSerializer想要的那样格式化XML。
尝试使用DataContractSerializer反序列化您的Entity类的实例,并确切了解XML应该是什么样子。