Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json WCF WebInvoke方法POST_Json_Wcf_Http Post - Fatal编程技术网

Json WCF WebInvoke方法POST

Json WCF WebInvoke方法POST,json,wcf,http-post,Json,Wcf,Http Post,我有一个wcf服务,我想测试向它发布数据。但是我函数的参数从来没有得到任何值 [OperationContract] [WebInvoke(UriTemplate = "TestPost", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.W

我有一个wcf服务,我想测试向它发布数据。但是我函数的参数从来没有得到任何值

[OperationContract]
[WebInvoke(UriTemplate = "TestPost", Method = "POST", 
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int Test(string value);

public int Test(string value)  //Value stays null
{
    return 0;
}
我发送的JSON请求,使用Fiddler2构建

http://localhost:49633/Service1.svc/TestPost

User-Agent: Fiddler
Host: localhost:49633
Content-Length: 42
Content-type: application/json

{"value":{"name":"value","name1":"value"}}
我希望参数包含一个JSON字符串,因此基本上我正在创建一个包含JSON对象的JSON请求,因为我希望稍后将JSON对象反序列化为一个自定义对象。你知道为什么value参数保持为null吗


谢谢

我正在使用下面的方法将json字符串发布到上面定义的服务中,它适合我:

我的服务:

[WebInvoke(UriTemplate = "TestPost", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public int Test(string value)
        {
            Console.Write(value);
            return 1;
        }
我的客户:

private string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string requestBody)
        {
            string responseMessage = null;                
            var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
            if (request != null)
            {                    
                request.ContentType = "application/json";
                request.Method = method;
            }

            //var objContent = HttpContentExtensions.CreateDataContract(requestBody);
            if(method == "POST" && requestBody != null)
            {                   
                byte[] requestBodyBytes = ToByteArrayUsingJsonContractSer(requestBody);                
                request.ContentLength = requestBodyBytes.Length;
                using (Stream postStream = request.GetRequestStream())
                    postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);

            }

            if (request != null)
            {
                var response = request.GetResponse() as HttpWebResponse;
                if(response.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        var reader = new StreamReader(responseStream);

                        responseMessage = reader.ReadToEnd();
                    }
                }
                else
                {
                    responseMessage = response.StatusDescription;
                }
            }
            return responseMessage;
        }

private static byte[] ToByteArrayUsingJsonContractSer(string requestBody)
        {
            byte[] bytes = null;
            var serializer1 = new DataContractJsonSerializer(typeof(string));
            var ms1 = new MemoryStream();
            serializer1.WriteObject(ms1, requestBody);
            ms1.Position = 0;
            var reader = new StreamReader(ms1);
            bytes = ms1.ToArray();
            return bytes;
        }
我对客户端的UseHttpWebApproach调用如下:

string serviceBaseUrl = <<your service base url>>;
string resourceUrl = "/TestPost";
string method = "POST";
string jsonText = "{\"value\":{\"name\":\"value\",\"name1\":\"value\"}}";
UseHttpWebApproach(serviceBaseUrl, resourceUrl, method, jsonText);

参数是字符串,因此其值也应该是字符串。尝试传递
{“value”:“{\“name\”:“value\”,\“name1\”:“value\”}
。虽然我很惊讶服务没有给你一个错误而不是一个空参数。你能试着把BodyStyle删除为WrappedRequest默认的值吗
POST http://localhost/VDName/AppName/TestPost HTTP/1.1
Content-Type: application/json
Content-Length: 54
Connection: Keep-Alive

"{\"value\":{\"name\":\"value\",\"name1\":\"value\"}}"