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

bubblemessengerSecurity

Nov 5, 2013 (4 years and 1 month ago)

76 views

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





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 Visu
al 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 a
pplication, 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='http://schemas.xmlsoap.org/soap/envelope/'>

<Body>

<find_business generic='1.0' xmlns='urn:uddi
-
org:api'>

<name>
Microsoft</name>

</find_business>

</Body>

</Envelope>


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;

xmlfile=Request.Params["xmlfile"];

if (xmlfile==null)

xmlfile="uddi.xml";

HttpSOAPRequest(xmlfile,null);

}


void HttpSOAPRequest(String xmlfile, string proxy)

{

XmlDocument doc = new XmlDocument();

doc.Load(@"C:
\
Inet
pub
\
wwwroot
\
ASP.NET
\
" +xmlfile);

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://uddi.microsoft.com/inquire");

if (proxy != null) req.Proxy = new WebProxy(proxy,true);

// if SOAPAction header is required, add it here...

req.Headers.Add("SOAP
Action","
\
"
\
"");


req.ContentType = "text/xml;charset=
\
"utf
-
8
\
"";

req.Accept = "text/xml";

req.Method = "POST";

Stream stm = req.GetRequestStream();

doc.Save(stm);

stm.Close();

WebResponse resp = req.GetResponse();

stm = resp.GetResponseStream();

StreamRea
der r = new StreamReader(stm);

// process SOAP return doc here. For now, we'll just send the XML out to the browser ...

Response.Write(r.ReadToEnd());

}

</script>

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 a
s 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("http://uddi.microsoft.com/inquire");
) , 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 proces
sing 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.