Writing SOAP request from scratch

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.

~ by blogesh on March 1, 2007.

19 Responses to “Writing SOAP request from scratch”

  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.. :)

  12. HttpWebRequest request = (HttpWebRequest)
    HttpWebRequest.Create(“http://btdevomneapp.btdev.com:12005/Services/VendorAPI.svc”);

    String xmlString = “http://tempuri.org/IVendorAPI/EarlyCheckin93D3368A-C3B0-42CD-977E-D97D3B388D9F”;
    ASCIIEncoding encoding = new ASCIIEncoding();

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

    request.Method = “POST”;
    request.ContentLength = bytesToWrite.Length;
    request.Headers.Add(“SOAPAction: \”http://tempuri.org/IVendorAPI/EarlyCheckin\”"); //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();

    —————————————————-

    I’m getting “The remote server returned an error: (400) Bad Request.” after executing.

    Any ideas why i’m getting this exception.

    thanks in advance.

  13. Hmm it looks like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new to everything. Do you have any recommendations for newbie blog writers? I’d really appreciate it.

  14. I simply couldn’t depart your web site before suggesting that I actually enjoyed the standard info a person provide for your guests? Is going to be back regularly to inspect new posts

  15. Nice replies in return of this matter with genuine arguments and telling the
    whole thing concerning that.

  16. Your own post features proven beneficial to me. It’s really useful and
    you are naturally quite well-informed in this field. You have got opened
    our face to be able to numerous views on this subject together with intriguing,
    notable and sound content material.

  17. Wonderful work! That is the type of info that are meant to
    be shared across the internet. Disgrace on the seek engines for no longer positioning this publish
    upper! Come on over and discuss with my site . Thank you
    =)

  18. Raw organic Argan oil helps replenish your skin and makes you
    look younger and rejuvenated almost instantly.
    I am always advocating to drink more orange juice and eat fewer pastries around my home.
    It may be applied on the hair of men who are suffering from
    hair thinning.

  19. […] http://blogesh.wordpress.com/2007/03/01/writing-soap-request-from-scratch/ […]

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.

%d bloggers like this: