How to construct a soap service request using C# for HTTPWebRequest? RRS feed

  • Question

  • Dear Folks,

    I'm writing here with query in consuming a soap service with HTTPWebRequest for Windows Phone application. I'm following the below to steps to construct the request and pass to the server. But i was not able to connect with this server and returns as "The remote server returned an error: NotFound." But i works fine with the Fiddler tool. Pleas can anyone suggest me where i'm wrong in the below implementation. 

    Step 1 

    -->  Creating a Request with required headers. (Can you please verify the "SOAPAction" header is added in correct way.)

      public void HttpSoapPost(string uri, AsyncCallback callBack)
                    ServerURL = uri;
                    uri = uri + "/Reports1.1/WS/Retrieve.svc/soap";  
    // uri value will be "https://onsite.soaptestingserver.com/Reports1.1/WS/Retrieve.svc/soap
                    var request = (HttpWebRequest)WebRequest.Create(uri);
                    request.Method = Common.Method;
                    request.ContentType = "text/xml; charset=utf-8";
                    request.Headers[HttpRequestHeader.ContentLength] = "297";
                    request.Headers[HttpRequestHeader.UserAgent] = "WP7";
                    request.Headers[HttpRequestHeader.Connection] = Common.Connection;
                    request.Headers[HttpRequestHeader.AcceptEncoding] = Common.AcceptEncoding;
                    request.Headers[HttpRequestHeader.Host] = "soaptestingserver.com:8082";
                    request.Headers[HttpRequestHeader.Expect] = "100-continue";
                    request.Headers["SOAPAction:"] = "https://onsite.soaptestingserver.com/Reports1.1/WS/Retrieve.svc/IRetrieve/GetVersions";  
                    request.BeginGetResponse(callBack, request);
                catch (Exception exp)
                    throw exp;

    Step 2: Send the request to add the body content. 

            private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
                // End the operation
                Stream postStream = request.EndGetRequestStream(asynchronousResult);
                //StreamWriter streamWriter = new StreamWriter(postStream, Encoding.UTF8);

                StringBuilder soapRequest = new StringBuilder("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
                soapRequest.Append("<GetVersions xmlns=\"https://www.onsite.soaptestingserver.com/Reports1.1/WS/Retrieve.svc\">");
                soapRequest.Append("<Request xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">");
                string postData = soapRequest.ToString();

                // Convert the string into a byte array.
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);

                // Write to the request stream.
                postStream.Write(byteArray, 0, postData.Length);

                // Start the asynchronous operation to get the response
                request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);

            private static void GetResponseCallback(IAsyncResult asynchronousResult)
                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

                // End the operation /* I'm getting the exception in the next line as "The remote server returned an error: NotFound."*/
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                string responseString = streamRead.ReadToEnd();
                // Close the stream object

                // Release the HttpWebResponse

    Thanks in advance. 
    Friday, July 13, 2012 8:29 PM


All replies