且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

400错误的请求发送XML有效载荷WCF REST服务

更新时间:2023-09-09 16:37:34

使用&LT你的第二个例子;串> 元素应该工作。如果你知道你是recieving的XML的架构,您可以执行以下操作:

Your second example using the <string> element should work. If you know the schema of the XML that you are recieving, you can do the following:

[WebInvoke(UriTemplate = "/cust_key/{key}/prod_id/{id}", 
       Method = "POST",
       BodyStyle = WebMessageBodyStyle.Bare, 
       RequestFormat = WebMessageFormat.Xml, 
       ResponseFormat = WebMessageFormat.Xml)]  
//Almost exacely the same except String is now Product in the method Parameters
Stream GetData(string key, string id, Product data);

[DataContract(Namespace = "")]
public class Prodect
{
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public string price { get; set; }
}

然后使用客户端code,从您的文章,它应该工作的罚款。在另一方面,如果你希望你的Web服务来动态地接受,没有明确定义,你可以使用的XElement数据契约不同的XML如下:

Then use the client code from your post and it should work fine. On the other hand, if you want your web service to dynamically accept different XML that are not well defined data contracts you can use XElement as follows:

[WebInvoke(UriTemplate = "/cust_key/{key}/prod_id/{id}", 
       Method = "POST",
       BodyStyle = WebMessageBodyStyle.Bare, 
       RequestFormat = WebMessageFormat.Xml, 
       ResponseFormat = WebMessageFormat.Xml)]  
//Almost exacely the same except String is now XElement
Stream GetData(string key, string id, XElement data);