400错误请求将xml有效负载发送到WCF REST服务

我知道有几篇post询问400错误,我相信我已经阅读了所有这些错误,但我认为我面临的问题是不同的。

这是我的WCF服务合同

[WebInvoke(UriTemplate = "/cust_key/{key}/prod_id/{id}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] Stream GetData(string key, string id, string data); 

这是我用来将请求发送到我的restsvc的代码

 request.RequestUri = new Uri("http://localhost:3138/v1/cust_key/company1/prod_id/testProductID"); request.ContentType = "application/xml"; request.HttpMethod = "POST"; string xml = @"dell 400400 dollars"; byte[] message = Encoding.ASCII.GetBytes(xml); string data = Convert.ToBase64String(message); response = request.MakeWebRequest(null, data); 

这给了我400个错误请求错误。 我试图将xml字符串更改为以下两个,它们也会产生400错误

  <![CDATA[dell 400400 dollars]]>  

要么

 <![CDATA[dell 400400 dollars]]> 

如果有效负载xml是空字符串,那么一切都很好,返回200。 任何人都可以帮我一把吗?

编辑:我的web.config部分。 它从WCF REST服务模板40(CS)开箱即用

     <!-- Configure the WCF REST service base address via the global.asax.cs file and the default endpoint via the attributes on the  element below -->    

使用元素的第二个示例应该可行。 如果您知道要接收的XML架构,则可以执行以下操作:

 [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; } } 

然后使用您post中的客户端代码,它应该可以正常工作。 另一方面,如果您希望Web服务动态接受不是明确定义的数据协定的不同XML,则可以使用XElement,如下所示:

 [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);