const string webserviceKey = "X";
const string webserviceUrl = "https://training.coachview.net/Webservice/Geavanceerd.asmx";
var parameters = new Dictionary<string, string>();
parameters.Add("intOpleidingId", "0");
var result = SoapRequest("SelecterenOpleiding", parameters) /// <summary>
/// Execute a Soap WebService call
/// <param name="action">Get / post action (SelecterenOpleiding, SelecterenOpleidingsOnderdelen)</param>
/// <param name="parameters">Dictionary with optional paramaters</param>
/// </summary>
private string SoapRequest(string action, Dictionary<string, string> parameters)
{
//Create Request
HttpWebRequest request = CreateWebRequest(action);
using (Stream stream = request.GetRequestStream())
{
//add soap body to stream
CreateSoapBody(action, parameters).Save(stream);
}
using (WebResponse response = request.GetResponse())
{
//read response
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
var soapResult = rd.ReadToEnd();
var doc = new XmlDocument();
doc.LoadXml(soapResult);
//get node with actual response
var result = doc.GetElementsByTagName($"{action}Result");
//create json from the node.
if(result != null)
{
return JsonConvert.SerializeXmlNode(result.Item(0));
}
return "";
}
}
}
/// <summary>
/// Create a body
/// </summary>
/// <param name="action">Get / post action</param>
/// <param name="parameters">Dictionary with optional paramaters</param>
/// <returns></returns>
private XmlDocument CreateSoapBody(string action, Dictionary<string, string> parameters)
{
var xml = new XmlDocument();
xml.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"<soap:Body>" +
$"<{action} xmlns=\"http://coachview.net/webservices\">" +
"<aWebserviceAuthentication>" +
$"<Key>{webserviceKey}</Key>" +
"</aWebserviceAuthentication>" +
$"{ CreateParameters(parameters)}" +
$"</{action}>" +
"</soap:Body>" +
"</soap:Envelope>");
return xml;
}
/// <summary>
/// create string from parameters
/// </summary>
/// <param name="parameters">dictionatry of key(string) value(string).</param>
/// <returns></returns>
private string CreateParameters(Dictionary<string, string> parameters)
{
var result = "";
foreach(KeyValuePair<string, string> param in parameters)
{
result += $"<{param.Key}>{param.Value}</{param.Key}>";
}
return result;
}
/// <summary>
/// Create a soap webrequest to webserviceUrl.
/// </summary>
/// <param name="parameters">action.</param>
/// <returns></returns>
private HttpWebRequest CreateWebRequest(string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(webserviceUrl);
webRequest.Headers.Add("SOAPAction", $"http://coachview.net/webservices/{action}");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
|