![](/img/trans.png)
[英]WCF Post Operation Contract returning 400 bad request For Android request
[英]WCF JSON POST request, single string parameter not binding and returning 400
在我的WCF(天蓝云)服务中,我想支持JSON。 我正在创建一些测试方法,看看是否一切正常。 我可以让GET调用工作,但是当我用一个简单的参数进行POST时,我总会得到:
The remote server returned an error: (400) Bad Request.
如果我不发送参数,它将执行该方法,但当然使用null值作为参数。 我尝试了不同格式的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);
黄金组合是在JSON代码中使用双引号并结合WebMessageBodyStyle.WrappedRequest。
工作JSON:
string jsonInput = "{\"data\":\"testvalue\"}";
将WebMessageBodyStyle设置为Bare时,以下JSON可以正常工作:
string jsonInput = "\"testvalue\"";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.