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

2023-09-03 17:12:49 作者:风光的背后是沧桑,?

我知道有几个帖子询问了400错误,我相信我读过所有的人,但我觉得我现在面临的问题是不同的。

I know there are a few posts asking about the 400 error, and I believe I've read all of them, but I think the problem I'm facing is different.

这是我的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);

这是我用来发送请求到我的休息code SVC

request.RequestUri = 
     new Uri("http://localhost:3138/v1/cust_key/company1/prod_id/testProductID");
request.ContentType = "application/xml";
request.HttpMethod = "POST";

string xml = 
         @"<Product><name>dell 400</name><price>400 dollars</price></Product>";

byte[] message = Encoding.ASCII.GetBytes(xml);
string data = Convert.ToBase64String(message);
response = request.MakeWebRequest(null, data);

这给了我400错误的请求错误。我试图XML字符串更改为以下两个,他们还生产400错误

This gave me 400 bad request error. I've tried to change the xml string to the following two and they also produce 400 error

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
       <![CDATA[<Product><name>dell 400</name><price>400 dollars</price></Product>]]>
</string>

<![CDATA[<Product><name>dell 400</name><price>400 dollars</price></Product>]]>

如果有效载荷XML为空字符串,那么一切都很好,200返回。谁能给我个忙吗?

If the payload xml is empty string, then everything is fine and 200 is returned. Can anyone give me a hand?

编辑:我的web.config部分。它散发出来的箱子从 WCF REST服务模板40(CS)

my web.config section. it comes out of box from the WCF REST Service Template 40(CS)

<system.serviceModel>
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  <standardEndpoints>
  <webHttpEndpoint>
    <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
    -->
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
  </webHttpEndpoint>
</standardEndpoints>

推荐答案

使用&LT你的第二个例子;串&GT; 元素应该工作。如果你知道你是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);