【发布时间】:2011-10-26 21:36:52
【问题描述】:
我是 wcf restful 服务的新手。我找不到问题,为什么我的 wcf restful 服务给出“错误请求”。我使用 .NET 4.0。
我的服务是:
[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 Bad Request”
我的提琴手请求头是:
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# 客户端程序调用这个服务就可以了。
我的客户端代码是:
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();
}
请帮帮我........
谢谢。
【问题讨论】: