Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 400向WCF REST服务发送xml负载的错误请求_C#_.net_Xml_Wcf_Rest - Fatal编程技术网

C# 400向WCF REST服务发送xml负载的错误请求

C# 400向WCF REST服务发送xml负载的错误请求,c#,.net,xml,wcf,rest,C#,.net,Xml,Wcf,Rest,我知道有一些帖子询问400错误,我相信我已经阅读了所有的帖子,但我认为我面临的问题是不同的 这是我的WCF服务合同 [WebInvoke(UriTemplate = "/cust_key/{key}/prod_id/{id}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml,

我知道有一些帖子询问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);
这是我用来向rest 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);
request.RequestUri=
新Uri(“http://localhost:3138/v1/cust_key/company1/prod_id/testProductID");
request.ContentType=“应用程序/xml”;
request.HttpMethod=“POST”;
字符串xml=
@“戴尔400400美元”;
byte[]message=Encoding.ASCII.GetBytes(xml);
字符串数据=Convert.tobase64字符串(消息);
response=request.MakeWebRequest(null,数据);
这给了我400个错误的请求。我尝试将xml字符串更改为以下两个,它们也会产生400错误

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

戴尔400400美元]]>

dell 400400美元]]>
如果有效负载xml是空字符串,则一切正常,返回200。谁能帮我一把吗

编辑:我的web.config部分。它是从盒子里拿出来的



使用
元素的第二个示例应该可以运行。如果您知道正在接收的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; }
}
然后使用文章中的客户端代码,它应该可以正常工作。另一方面,如果希望web服务动态接受不同的XML,这些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);

REST不意味着请求中没有任何有效负载,所有内容都在URL中???@Mithrandir REST不关心如何传递有效负载。@abraham:REST范式不关心,但WCF关心@Mithrandir,那么如何将数据发布到WCF REST服务?WCF REST支持POST,所以它必须接受请求负载。@Laguna Nvm我的最后一条评论。它可能是内容类型。。尝试“text/xml”。是否可以在没有产品数据合同的情况下执行此操作?产品xml简化了我真正的业务问题。在实际场景中,不可能为xml负载设计datacontract。我只是在我自己的测试服务器上试过,它可以工作,所以我不知道为什么它对你不起作用。注意:我为我的客户端使用了fiddler。我将只更新我的解决方案以允许任意XML而不是定义良好的对象。我使用fiddler 2测试了代码,您可以在此处下载:如果仍然遇到问题,请使用相关的web.config--system.serviceModel.let更新您的问题
[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; }
}
[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);