首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF WebInvoke方法POST

WCF WebInvoke方法POST
EN

Stack Overflow用户
提问于 2012-03-15 15:51:39
回答 1查看 36K关注 0票数 3

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

代码语言:javascript
复制
[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构建

代码语言:javascript
复制
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参数保持为空吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-15 18:17:09

我使用下面的方法将json字符串发送到上面定义的服务,它对我有效:

我的服务:

代码语言:javascript
复制
[WebInvoke(UriTemplate = "TestPost", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public int Test(string value)
        {
            Console.Write(value);
            return 1;
        }

我的客户:

代码语言:javascript
复制
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客户端的调用如下:

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

以下是我的Fiddler请求:

代码语言:javascript
复制
POST http://localhost/VDName/AppName/TestPost HTTP/1.1
Content-Type: application/json
Content-Length: 54
Connection: Keep-Alive

"{\"value\":{\"name\":\"value\",\"name1\":\"value\"}}"
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9715743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档