h1

Writing SOAP request from scratch

March 1, 2007

I had to test a few web services today and the test data was in the form of several XML files containing the actual SOAP envelope. I googled to see if I could find a simple enough tool on the web with no success. Eventually, I had to write my own :( .

Here is a bit of code that may come in handy to someone someday -

    HttpWebRequest request = (HttpWebRequest) 
                            HttpWebRequest.Create(url); 

    String xmlString = txtInput.Text; 
    ASCIIEncoding encoding = new ASCIIEncoding(); 

    byte[] bytesToWrite = encoding.GetBytes(xmlString); 

    request.Method = "POST"; 
    request.ContentLength = bytesToWrite.Length; 
    request.Headers.Add("SOAPAction: \"http://localhost/XXX/ActionName\""); //You need to change this 
    request.ContentType = "text/xml; charset=utf-8"; 

    Stream newStream = request.GetRequestStream(); 
    newStream.Write(bytesToWrite, 0, bytesToWrite.Length); 
    newStream.Close(); 

    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
    Stream dataStream = response.GetResponseStream(); 
    StreamReader reader = new StreamReader(dataStream); 

    string responseFromServer = reader.ReadToEnd();

The code is actually quite straight forward. I create a HttpWebRequest and set some basic settings such as the content length and type. The one key thing is to add the SOAPAction in the http’s header. Once that is done, all we need to do is send the request to the url.

11 comments

  1. Hi there I have a similar problem here and I am trying to do the exact same. I’ve used your code but I have no idea what to substitute request.Headers.Add(“SOAPAction: \”http://localhost/XXX/ActionName\”"); by…

    do you have any ideas…?


  2. If you have an asmx file, open it and click on the operation – You will find the SOAPAction parameter there…


  3. Thank you. That was very useful


  4. Thanks, very usefull!


  5. How can I retrieve soap request for a web method on a button click. I need to display it in a textbox.


  6. Is there anyway of creating this same setup through WCF without using svcutil?


  7. ROCKS!!!! Saved me a lot of time :)
    The only thing you might want to indciate is the needed namespaces (for the un-initiated like myself.) Eg:
    using System;
    using System.Net; // added for HttpWebRequest/Response
    using System.IO; // needed for Stream type
    using System.Text; // needed for ASCIIEncoding


  8. Came in very handy today. Needed to specify the SoapAction. Many thanks.


  9. I can use this code to send xml message soap to url, not is web service the target.


  10. i create a simple asmx web service C# that has a method that returns an int.
    However, the java client while calling the web service added the following
    They added the following to the request headers:
    post.addRequestHeader(“Authorization”, “BASIC ” + getUserEncoding());
    post.addRequestHeader(“Content-type”, “text/xml; charset=UTF-8″);
    post.addRequestHeader(“SOAPAction”, “initiate”);

    and they get the below error.
    what should be done at my end to ensure that the error goes away.


  11. Thank you very much… it was very useful.. :)



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.