A newer version of this documentation is available.

View Latest

RequestClient.cs

Contains the function for sending SOAP requests, call on by the invokemessage functions with given soap-envelopes.

// ------------------------------------------------------------------------------------------
// <copyright file="RequestClient.cs" company="SecMaker AB">
//   SecMaker AB
// </copyright>
// <summary>
//   Defines the RequestClient class.
// </summary>
// ------------------------------------------------------------------------------------------

namespace SecMaker.NiP.Client
{
	using System;
	using System.IO;
	using System.Net;
	using System.Text;
	using System.Xml;

	internal class RequestClient
	{
		internal static string Url { get; set; }
		internal static string ActionName { get; set; }
		internal static string ContentType { get; set; }
		internal static HttpVerbEnum Method { get; set; }
		internal static string DataToSend { get; set; }

		public static string InvokeClient()
		{
			var xDoc = new XmlDocument();
			xDoc.LoadXml(DataToSend);

			var request = (HttpWebRequest) WebRequest.Create(Url);
			request.ContentType = ContentType;
			request.Method = Method.ToString();
			request.Headers.Add("SOAPAction", "\"http://www.secmaker.com/NiP/IServiceSoap/" + ActionName + "\"");

			using (var stream = request.GetRequestStream())
			{
				xDoc.Save(stream);
			}

			var toReturn =
				string.Empty;

			try
			{
				using (var response = request.GetResponse())
				{
					using (var stream = response.GetResponseStream())
					{
						if (stream != null)
						{
							using (var sr = new StreamReader(stream, Encoding.UTF8))
							{
								toReturn = sr.ReadToEnd();
							}
						}
					}
				}
			}
			catch (WebException err)
			{
				using (var response = err.Response)
				{
                    if (response != null)
                    {
                        var httpResponse =
                        (HttpWebResponse)response;
                        Console.WriteLine(@"Error code: {0}", httpResponse.StatusCode);

                        using (var stream = response.GetResponseStream())
                        {
                            if (stream != null)
                            {
                                using (var reader = new StreamReader(stream))
                                {
                                    var text = reader.ReadToEnd();
                                    Console.WriteLine(text);
                                }
                            }
                        }
                    }

				}
			}
			catch (Exception err)
			{
				Console.WriteLine(err.Message);
			}

			return toReturn;
		}

		public enum HttpVerbEnum
		{
			// ReSharper disable InconsistentNaming

			/// <summary>
			/// HTTP verb: GET.
			/// </summary>
			GET,

			/// <summary>
			/// HTTP verb: POST.
			/// </summary>
			POST,

			/// <summary>
			/// HTTP verb: UPDATE.
			/// </summary>
			UPDATE,

			/// <summary>
			/// HTTP verb: DELETE.
			/// </summary>
			DELETE

			// ReSharper restore InconsistentNaming
		}
	}
}