WCF WebInvoke Method POST

*爱你&永不变心* 提交于 2019-12-03 10:04:01

I am using the below method to post the json string to the service defined above and it works for me:

My service:

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

My Client:

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;
        }

My call on the client to the UseHttpWebApproach is as below:

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

Below is my Fiddler Request:

POST http://localhost/VDName/AppName/TestPost HTTP/1.1
Content-Type: application/json
Content-Length: 54
Connection: Keep-Alive

"{\"value\":{\"name\":\"value\",\"name1\":\"value\"}}"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!