Nee*_*Car 8 .net c# rest wcf json
在我的WCF(天蓝云)服务中,我想支持JSON.我正在创建一些测试方法,看看是否一切正常.我可以让GET调用工作,但是当我用一个简单的参数进行POST时,我总会得到:
The remote server returned an error: (400) Bad Request.
Run Code Online (Sandbox Code Playgroud)
如果我不发送参数,它将执行该方法,但当然使用null值作为参数.我尝试了不同格式的JSON和WebMessageBodyStyle,但似乎都没有.
如果我将参数类型更改为Stream我接收数据,但我必须手动反序列化它.这不应该是必要的吗?
接口:
[OperationContract]
[WebInvoke(UriTemplate = "Test",
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string Test(string data);
Run Code Online (Sandbox Code Playgroud)
IMPL:
public string Test(string data)
{
return "result is " + data;
}
Run Code Online (Sandbox Code Playgroud)
测试客户:
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);
Run Code Online (Sandbox Code Playgroud)
Nee*_*Car 10
黄金组合是在JSON代码中使用双引号并结合WebMessageBodyStyle.WrappedRequest.
工作JSON:
string jsonInput = "{\"data\":\"testvalue\"}";
Run Code Online (Sandbox Code Playgroud)
将WebMessageBodyStyle设置为Bare时,以下JSON可以正常工作:
string jsonInput = "\"testvalue\"";
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5176 次 |
最近记录: |