ASP.Net : Construct a test Soap Request using sample

I recently did a project involving ASP.Net web services. I created one of my own and had to call it from some other part of the same project. While  debugging there were numerous occasions when I needed to build small Soap requests to test out functionality or even connectivity. It was annoying to write the Soap packets manually and it was extra-annoying when I could simply get a dummy Soap request from the web service page provided by ASP.NET(screenshot below).

Suppose, I have a simple (VB.Net) method in my web service that I want to call from somewhere:

<WebMethod()> _
Public Function HelloWorld(ByVal str As String) As String
    Return str
End Function

When I run the web service and click the "HelloWorld" link I get the following yellow looking page, which is not new to ASP.Net developers :

This page contains the sample SOAP request/response to call the web method. To use this effectively, I created a little HTML page with some javascript code that takes the sample request text as argument and returns a code that assigns this to a string. Here is the code:

function generateCode()
   var a = document.getElementById('txtSoap');
   var count = 0;
   var output = 'var str = \'\';\rstr += \'';
   for(var i=0;i<a.value.length;i++)
     if(a.value.charAt(i)=='\r' || a.value.charAt(i)=='\n')
     output += '\';';
     output += a.value.charAt(i);
     if(a.value.charAt(i)=='\r' || a.value.charAt(i)=='\n')
     output += 'str += \'';
   output += '\';';
   txtOutput.value = output;

Upon giving this input:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="">
  <HelloWorld xmlns="">

We get this output:

var str = '';
str += '<?xml version="1.0" encoding="utf-8"?>';
str += '<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="">';
str += '<soap:Body>';
str += '  <HelloWorld xmlns="">';
str += '    <str>string</str>';
str += '  </HelloWorld>';
str += '</soap:Body>';
str += '</soap:Envelope>';
str += '';

Useful. Isn’t it? I ended up using it 3 or 4 times the same day.


, , , ,

  1. Leave a comment

Leave a Reply

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

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

Google photo

You are commenting using your Google 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

%d bloggers like this: