在我的WCF(天蓝色云)服务中,我想支持JSON。我正在创建一些测试方法来查看是否一切正常。我可以让 GET 调用正常工作,但是当我使用简单参数执行 POST 时,我总是会得到:
The remote server returned an error: (400) Bad Request.
如果我不发送参数,它将执行该方法,但当然会使用空值作为参数。我尝试了不同格式的 JSON 和 WebMessageBodyStyle,但似乎都不起作用。
如果我将参数类型更改为 Stream,我会收到数据,但我必须手动反序列化它。这应该没有必要吧?
界面:
[OperationContract]
[WebInvoke(UriTemplate = "Test",
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string Test(string data);
Impl:
public string Test(string data)
{
return "result is " + data;
}
测试客户端:
WebClient client = new WebClient();
client.Headers["Content-type"] = "application/json";
client.Encoding = System.Text.Encoding.UTF8;
string jsonInput = "{'data':'testvalue'}";
string postResponse = client.UploadString(postUrl, jsonInput);
Console.WriteLine("post response: " + postResponse);