我是wcf寧靜服務的新人。我無法找到問題所在,爲什麼我的健康服務給予'不好的請求'。我使用.NET 4.0。如何通過JSON請求從fiddler調用wcf restful服務?
我的服務是:
[OperationContract(Name="Add")]
[WebInvoke(UriTemplate = "test/", Method = "POST",
ResponseFormat=WebMessageFormat.Json,
RequestFormat=WebMessageFormat.Json)]
public int Add(Number n1)
{
res = Convert.ToInt32(n1.Number1) + Convert.ToInt32(n1.Number2);
return res;
}
的數據是..
[Serializable]
public class Number
{
public int Number1 { get; set; }
public int Number2 { get; set; }
}
當我從小提琴手它的迴歸呼籲 'HTTP/1.1 400錯誤的請求'
我的提琴手請求標題是:
User-Agent: Fiddler
Host: localhost:4217
Content-Type: application/json; charset=utf-8
而請求體是:
{"Number1":"7","Number2":"7"}
和響應報頭是:
HTTP/1.1 400 Bad Request
Server: ASP.NET Development Server/10.0.0.0
Date: Sun, 14 Aug 2011 18:10:21 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 5450
Cache-Control: private
Content-Type: text/html
Connection: Close
但是,如果我用C#的客戶端程序中調用這個服務就ok了。
我的客戶端代碼:
uri = "http://localhost:4217/Service1.svc/";
Number obj = new Number() { Number1 = 7, Number2 = 7 };
using (HttpResponseMessage response = new HttpClient().Post(uri+"test/",
HttpContentExtensions.CreateDataContract(obj)))
{
string res = response.Content.ReadAsString();
return res.ToString();
}
請幫我........
感謝。
的「正常」使用WCF REST是使用' '行爲(或者'WebHttpBehavior',如果通過代碼的話),而不是你在示例中使用的' '對於 ,默認的BodyStyle是'Bare',這意味着你不需要用帶參數名稱的JSON對象包裝對象(默認的 是'WrappedRequest')。 –
carlosfigueira
Carlos,感謝您的澄清! –