WCF JSON POST请求,单个字符串参数未绑定并返回400

NeedACar

在我的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);
NeedACar

黄金组合是在结合WebMessageBodyStyle.WrappedRequest的JSON代码中使用双引号。

工作的JSON:

   string jsonInput = "{\"data\":\"testvalue\"}";

将WebMessageBodyStyle设置为Bare时,以下JSON起作用:

   string jsonInput = "\"testvalue\"";

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章