SOAP (Simple Object Access Protocol) is a protocol used for exchanging structured information in web services. In this article, we will explore practical examples of SOAP API client creation in C#. These examples will guide you through different contexts to help you understand how to effectively interact with SOAP APIs using C#.
In this example, we will create a simple SOAP client that communicates with a weather web service. This client will send requests to retrieve weather data based on a city name.
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
class Program
{
static async Task Main(string[] args)
{
var soapEnvelope = new XElement("soap:Envelope",
new XAttribute(XNamespace.Xmlns + "soap", "http://schemas.xmlsoap.org/soap/envelope/"),
new XElement("soap:Body",
new XElement("GetWeather",
new XElement("CityName", "New York")
)
)
);
using (var httpClient = new HttpClient())
{
var content = new StringContent(soapEnvelope.ToString(), System.Text.Encoding.UTF8, "text/xml");
var response = await httpClient.PostAsync("http://www.webservicex.net/globalweather.asmx", content);
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
}
}
This example demonstrates creating a SOAP client that requires authentication to access a secure web service. We will use basic authentication to send credentials along with our request.
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xml.Linq;
class Program
{
static async Task Main(string[] args)
{
var soapEnvelope = new XElement("soap:Envelope",
new XAttribute(XNamespace.Xmlns + "soap", "http://schemas.xmlsoap.org/soap/envelope/"),
new XElement("soap:Body",
new XElement("GetUserDetails",
new XElement("UserId", "12345")
)
)
);
using (var httpClient = new HttpClient())
{
var byteArray = System.Text.Encoding.ASCII.GetBytes("username:password");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var content = new StringContent(soapEnvelope.ToString(), System.Text.Encoding.UTF8, "text/xml");
var response = await httpClient.PostAsync("https://secure.example.com/service.asmx", content);
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
}
}
In this example, we will create a SOAP client that sends custom headers along with our request. This can be useful for scenarios where the web service requires specific metadata to process the request.
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xml.Linq;
class Program
{
static async Task Main(string[] args)
{
var soapEnvelope = new XElement("soap:Envelope",
new XAttribute(XNamespace.Xmlns + "soap", "http://schemas.xmlsoap.org/soap/envelope/"),
new XElement("soap:Body",
new XElement("CalculateTax",
new XElement("Amount", "1000")
)
)
);
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Client-ID", "123456");
httpClient.DefaultRequestHeaders.Add("Api-Key", "abcdef");
var content = new StringContent(soapEnvelope.ToString(), System.Text.Encoding.UTF8, "text/xml");
var response = await httpClient.PostAsync("http://api.example.com/taxservice.asmx", content);
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
}
}