none
How to make a WCF Service accept a post data? RRS feed

  • Question

  • Hi, Wish everything is good at your side.

    I've been trying for a long hours to find a solution 

    I'm trying to make a WCF service to accept POST Data, For this I'm using the codes below, But It works only in localhost:

    Interface(IService):

     [OperationContract(Name = "PostSampleMethod")]
            [WebInvoke(Method = "POST",
             UriTemplate = "PostSampleMethod/New")]
            string PostSampleMethod(Stream data);

    Service Class:

    public string PostSampleMethod(Stream data)
        {
            // convert Stream Data to StreamReader
            StreamReader reader = new StreamReader(data);
            // Read StreamReader data as string
            string xmlString = reader.ReadToEnd();
            string returnValue = xmlString;
            // return the XMLString data
            return returnValue;
        }

    then, When I'm testing this service in localhost(http://localhost/MYWCF/Service.svc/PostSampleMethod/New) it works perfectly.

    // Restful service URL
                string url ="http://localhost/MYWCF/Service.svc/PostSampleMethod/New";
     
    
                // declare ascii encoding
                ASCIIEncoding encoding = new ASCIIEncoding();
                string strResult = string.Empty;
                // sample xml sent to Service & this data is sent in POST
                string SampleXml = @"<parent>" +
                        "<child>" +
                            "<username>username</username>" +
                            "<password>password</password>" +
                        "</child>" +
                      "</parent>";
                string postData = SampleXml.ToString();
                // convert xmlstring to byte using ascii encoding
                byte[] data = encoding.GetBytes(postData);
                // declare httpwebrequet wrt url defined above
                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
                // set method as post
                webrequest.Method = "POST";
                // set content type
                webrequest.ContentType = "application/x-www-form-urlencoded";
                // set content length
                webrequest.ContentLength = data.Length;
                // get stream data out of webrequest object
                Stream newStream = webrequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                // declare & read response from service
                HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
    
                // set utf8 encoding
                Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                // read response stream from response object
                StreamReader loResponseStream = 
    		new StreamReader(webresponse.GetResponseStream(), enc);
                // read string from stream data
                strResult = loResponseStream.ReadToEnd();
                // close the stream object
                loResponseStream.Close();
                // close the response object
                webresponse.Close();

    But When I'm testing it using my site url(http://mysite.com/MYWCF/Service.svc/PostSampleMethod/New) it occurred the following error:

    The resource cannot be found.

    Any thoughts please, how can I make it work.

    Thank You


    Tuesday, October 14, 2014 4:19 PM

Answers

All replies