Posts Tagged SOAP

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="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
  <HelloWorld xmlns="http://tempuri.org/">
    <str>string</str>
  </HelloWorld>
</soap:Body>
</soap:Envelope>

We get this output:

var str = '';
str += '<?xml version="1.0" encoding="utf-8"?>';
str += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
str += '<soap:Body>';
str += '  <HelloWorld xmlns="http://tempuri.org/">';
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.

, , , ,

Leave a comment