WCF REST adding data using POST or PUT 400 Bad Request
HI How do i add data using wcf rest architecture. I dont want to use the channelfactory to call my method. Something similar to the webrequest and webresponse used for GET. Something similar to the ajax WebServiceProxy restInvoke Or do i always have to use the Webchannelfactory implementation
I am getting a 400 BAD request by using the following
Dim url As String = "http://localhost:4475/Service.svc/Entity/Add" Dim req As WebRequest = 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开发者_StackOverflow中文版://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
thanks
In REST, you create a resource (ie, add data) either by using HTTP POST (if the server assigns the resource name) or HTTP PUT (if the client is assigning the resource name). You update a resource using PUT, and you delete it using DELETE. Only the HTTP method changes.
Rob Bagby has an 11 part series on REST in WCF.
I suspect you may be either missing some namespaces in your XML or you are not formating the XML as the DataContractSerializer wants it.
Try just using the DataContractSerializer to deserialize an instance of your Entity class and see exactly what the XML is supposed to look like.
精彩评论