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.

Advertisements

28 responses

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

  3. 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.

  4. 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.

  5. 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.

  6. 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

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

  8. 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
    =)

  9. 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.

  10. Pingback: 请问如何用c#直接发送soap请求 | 技术控

  11. Pingback: 请问如何用c#直接发送soap请求 - C# - 开发者问答

  12. The following tips can help maintain your skin during menopause and beyond:.
    They also, have anti-inflammatory and anti-bacterial properties and
    are full of nutrients and healing powers. It retains its natural glycerine, one of the best skin softeners.

  13. Have you ever thought about writing an ebook or guest authoring on other blogs?
    I have a blog based upon on the same information you discuss and would really
    like to have you share some stories/information.
    I know my visitors would enjoy your work. If you are even
    remotely interested, feel free to send me an e mail.

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 )

Google+ photo

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

Connecting to %s