[英]uploading large xml to WCF REST service -> 400 Bad request
[英]400 bad request sending xml payload to WCF REST service
我知道有一些帖子詢問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);
這是我用來將請求發送到我的休息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錯誤
<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。 任何人都可以幫我一把嗎?
編輯:我的web.config部分。 它從WCF REST服務模板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>
你的第二個使用<string>
元素的例子應該可行。 如果您知道要接收的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,則可以使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.