Invoking Web Service dynamically using HttpWebRequest

Yesterday I needed to implement (quickly) a mechanism of dynamic invocation of a web service. I called a Web Service using HttpWebRequest and gathered the response stream. I didn't know the description of the WS (WSDL) in design time. I added some configuration mechanisms to my application tha allow me to change the invoked web service without necesity of recompilation (I will show only the mechanics of WS invocation). How did I invoke this:

Step 1. My Web Service looks like this:

[WebService(Namespace = "http://tempuri.org/")\] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class CustomerWebService : System.Web.Services.WebService { [WebMethod] public string Register(long id, string data1) { return "ID.CUSTOMER"; } }

 Step 2. When opened in the Internet Explorer the IIS generates page for my Register method with samples of request. Here is such sample (the olive text is headers description):

POST /WebServices/CustomerWebService.asmx HTTP/1.1 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/Register"

<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 long string </soap:Body> </soap:Envelope>

Step 3. Create HttpWebRequest passing the WS url and soap action (similar to method name) and execute the request.

string soap = @" <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 <Register xmlns=""http://tempuri.org/""> 123 string </soap:Body> </soap:Envelope>";

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/WebServices/CustomerWebService.asmx"); req.Headers.Add("SOAPAction", "\"http://tempuri.org/Register\\""); req.ContentType = "text/xml;charset=\"utf-8\""; req.Accept = "text/xml"; req.Method = "POST";

using (Stream stm = req.GetRequestStream()) { using (StreamWriter stmw = new StreamWriter(stm)) { stmw.Write(soap); } }

WebResponse response = req.GetResponse();

Stream responseStream = response.GetResponseStream(); // TODO: Do whatever you need with the response

This type of code I call AGILE PIECE OF CODE - it is good enough to solve some problem although it isn't fancy.

This article is part of the GWB Archives. Original Author: Marcin Celej

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.