Building Dynamic SOAP Requests with ASP.NET [C#]

By Peter A. Bromberg, Ph.D.

Peter Bromberg  

One of the less difficult but interesting problems in using ASP.NET is how to make dynamic SOAP calls from within an ASP.NET page, but without all the overhead of building an ASP.NET application in Visual Studio.NET. The key here is to create a custom function that will accept the name of a SOAP Body XML file for the request, along with any parameters that need to be passed in from the QueryString, and then dynamically assemble the SOAP call and send it out with the WebRequest Class.

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP. This is a very easy class to use, and the overall methodology is very similar to XMLHTTP.

So what we'll do here is "borrow" from some nice sample code that was originally posted by Chris Lovett of Microsoft in one of the .NET newsgroups, but instead of using it in a console application, we'll design it in an ASPX page that can get the parameters off the querystring.

First, we need a SOAP body to send, and in this case we'll be sending a SOAP call to the Microsoft UDDI search service, making a "Business Generic" search on the name "MIcrosoft":

<?xml version='1.0' encoding='UTF-8'?>
<!-- Note that the Body contents could be any valid SOAP request -->
<Envelope xmlns=''>
<find_business generic='1.0' xmlns='urn:uddi-org:api'>

You can save this to your web folder as "uddi.xml".

Now let's construct the ASPX page that will receive the querystring parameters, send out the request, and return the result:

<% @ Page Language="C#" %>
<% @Import Namespace="System" %>
<% @Import Namespace="System.Xml" %>
<% @Import Namespace="System.Text" %>
<% @Import Namespace="System.IO" %>
<% @Import Namespace="System.Net" %>

<script Language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
string xmlfile;
if (xmlfile==null)

void HttpSOAPRequest(String xmlfile, string proxy)
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Inetpub\wwwroot\ASP.NET\" +xmlfile);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("");
if (proxy != null) req.Proxy = new WebProxy(proxy,true);
// if SOAPAction header is required, add it here...

req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
Stream stm = req.GetRequestStream();
WebResponse resp = req.GetResponse();
stm = resp.GetResponseStream();
StreamReader r = new StreamReader(stm);
// process SOAP return doc here. For now, we'll just send the XML out to the browser ...

What we do above is get the xml file for the SOAP Body from the QueryString (xmlfile=Request.Params["xmlfile"];), filling in a default one if it is null, and then immediately pass this information to the HttpSOAPRequest function. Note there is a parameter for a proxy, which is not used here, so we pass in a null for that parameter. We could just as easily pass in a number of Querystring parameters including the function values we want to send up, and even a value for the SOAPAction header.

Then we load our XML file ( doc.Load(@"C:\Inetpub\wwwroot\ASP.NET\" +xmlfile)).
We, set up our HttpWebRequest ((HttpWebRequest)WebRequest.Create("");) , the ContentType ("text/xml;charset=\"utf-8\"";), the accept type and Method "POST", and get our RequestStream in the stm Stream type variable and save it to the Stream.

Finally, we get our WebResponse from Microsoft in "resp", stick the results in a StreamReader ( StreamReader r = new StreamReader(stm);) and write it out to the page with Response.Write(r.ReadToEnd());.

Of course, in this example I haven't actually done any processing with the return document except to display it. You would want to get the values to use in other functions, or do a transform to display as HTML, etc. at this point in your code.

You can get a lot more sophisticated with this. You could put code like this in your global.asax and save the result to the Cache object, creating a file or other dependency that would force it to refresh at a certain interval. You get the idea. Enjoy.

Peter Bromberg is an independent consultant specializing in distributed .NET solutionsa Senior Programmer /Analyst at in Orlando and a co-developer of the developer website. He can be reached at