Home » Technology » C# » A C# Example For Querying Data From A SOAP Web Service Without A Service Reference

A C# Example For Querying Data From A SOAP Web Service Without A Service Reference

Continuing on my last article, which showed a simple SOAP request in C#, this article will do the same without using a Service Reference. The IDE I am using is Visual Studio Community Edition. The core technologies here are C#, and SOAP. I presume a familiarity with C#. SOAP, or Simple Object Access Protocol, is a Web Service which utilizes XML as it’s format and, in our examples, the data is transmitted via HTTP. As such, I presume a basic understanding of HTTP, and general Markup as you might find in HTML or XML.

The WSDL file

Our query against the SOAP web service will be dictated by the WSDL file provided for the web service in question. The WSDL file provides a contract, or user manual, listing the available functions we can call, along with information such as input data we can provide, and data we can expect to be returned.

I will be using a publicly available SOAP Web Service, and as such, it’s WSDL file; the file can be viewed in your browser by following the link, or downloaded and opened for analysis using Notepad, or Notepad++ in a Microsoft Windows environment:

http://footballpool.dataaccess.eu/data/info.wso?wsdl

Reviewing the WSDL file, we can see a number of functions; here is a simple one with one input variable:

<xs:element name=”AllMidFields>
<xs:complexType>
<xs:sequence>
<xs:element name=”sCountryName type=”xs:string/>
</xs:sequence>
</xs:complexType>
</xs:element>
The parent tag lists the function name: AllMidFields; and the input is a String, sCountryName. It is that simple; there is a function AllMidFields that takes a single input, the Country Name (sCountryName).
In Visual Studio, create a Console Application: File > New > Project > Console Application. We will be using two classes. The first and most important is for the WebService interaction:
using System.IO;
using System.Net;
using System.Web;
using System.Xml.Linq;
using System.Collections.Generic;namespace ConsoleApplication1
{

public class WebService
{
public string Url { get; set; }
public string MethodName { get; set; }
public string WebServiceNamespace { get; set; }
public Dictionary<string, string> Params = new Dictionary<string, string>();
public XDocument ResultXML;
public string ResultString;

public WebService()
{

}

public WebService(string url, string webservicenamespae, string methodName)
{
Url = url;
WebServiceNamespace = webservicenamespae;
MethodName = methodName;
}

/// <summary>
/// Invokes service
/// </summary>
public void Invoke()
{
Invoke(true);
}

/// <summary>
/// Invokes service
/// </summary>
/// <param name=”encode”>Added parameters will encode? (default: true)</param>
public void Invoke(bool encode)
{
string soapStr =
@”<?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>
<{0} xmlns=””” + WebServiceNamespace + @”””>
{1}
</{0}>
</soap:Body>
</soap:Envelope>”;

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
req.Headers.Add(“SOAPAction”, “\”” + WebServiceNamespace + “” + MethodName + “\””);
req.ContentType = “text/xml;charset=\”utf-8\””;
req.Accept = “text/xml”;
req.Method = “POST”;

using (Stream stm = req.GetRequestStream())
{
string postValues = “”;
foreach (var param in Params)
{
if (encode)
postValues += string.Format(“<{0}>{1}</{0}>”, HttpUtility.UrlEncode(param.Key), HttpUtility.UrlEncode(param.Value));
else
postValues += string.Format(“<{0}>{1}</{0}>”, param.Key, param.Value);
}

soapStr = string.Format(soapStr, MethodName, postValues);
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soapStr);
}
}

if ((req).Equals(null))
{
ResultString = “”;
}
else
{
using (StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream()))
{
string result = responseReader.ReadToEnd();
ResultXML = XDocument.Parse(result);
ResultString = result;
}
}
}
}
}

This class is modified from the original found here. The code to instantiate the above class and call the SOAP Web Service method is as follows:

using ConsoleApplication1.ServiceReference1;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebService ws = new WebService(“http://footballpool.dataaccess.eu/data/info.wso?wsdl”, “http://footballpool.dataaccess.eu”, “AllMidFields”);
ws.Params.Add(“sCountryName”, “Australia”);
ws.Invoke();

if (!(ws.ResultString).Equals(null))
{
System.Console.WriteLine(“{0}”, (ws.ResultString));
}
else
{
System.Console.WriteLine(“{0}”, “No data returned.”);
}
System.Threading.Thread.Sleep(100000);
}
}
}

The results returned are shown below:
C# No Service Reference SOAP Request Results
All content is © copyrighted by the owner of this website, 2014 and beyond | Check us out on Twitter