search
Twitter Rss Feeds
MicrosoftArticlesForumsGroups
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml/Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsGroups
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsGroups
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsGroups
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsGroups
Microsoft Excel
Microsoft Word
Microsoft Powerpoint
Publisher
Money

Operating SystemsArticlesForumsGroups
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsGroups
Share Point
BizTalk
Site Server
Exhange Server
IIS
Transaction Server

Graphic DesignArticlesForumsGroups
Macromedia Flash
Adobe PhotoShop
Microsoft Expression

OtherArticlesForumsGroups
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Reviews
Search Engines
Resumes

 
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='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:\Inetpub\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("SOAPAction","\"\"");

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();
StreamReader 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 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("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 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 EggheadCafe.com developer website. He can be reached at pbromberg@yahoo.com

Do you have a question or comment about this article? Have a programming problem you need to solve? Post it at eggheadcafe.com forums and receive immediate email notification of responses.


Pete's Blog   |    Pete's Resume   |    Robbe's Blog   |    Robbe's Resume   |    Archive #2   |    Archive #3   |    Dotnetslackers   |    XmlPitStop   |    Advertise   |   Contact Us   |   Privacy   |   Copyright (c) 2000 - 2009 eggheadcafe.com  All rights reserved.