Call a Web Service Without Adding a Web Reference

Jak
Jak
908 Points
132 Posts

Hi,

How to Call a Web Service Without Adding a Web Reference?

Views: 13562
Total Answered: 1
Total Marked As Answer: 1
Posted On: 01-Jan-2015 16:06

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
4916 Points
27 Posts
         

Hi,

ExampleWebSerive();

public void ExampleWebSerive()
{
string ResultString;
WebService WebServiceAPI = new WebService("https://example.com/ServiceRequestPort");
WebServiceAPI.AddParameter("id", "1"); // Case Sensitive! To avoid typos, just copy the WebMethod's signature and paste it
try
{
WebServiceAPI.Invoke("updateEliteCard"); // name of the WebMethod to call (Case Sentitive again!)
ResultString = "ResultString:" + WebServiceAPI.ResultString;
}
catch (Exception ex)
{
ResultString = "Error Message: <br>" + ex.Message + "<br>" + ex.InnerException + "<br><br> StackTrace: <br>" + ex.StackTrace + "<br>" + ex.Source;
}
finally { WebServiceAPI.CleanLastInvoke(); }
}

 

Uses two class (WebService,Utils):

 

//========= WebService.class =======================

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
public class WebService
{
public string Url { get; private set; }
public string Method { get; private set; }
public Dictionary<string, string> Params = new Dictionary<string, string>();
public XDocument ResponseSOAP;
public XDocument ResultXML;
public string ResultString;
public WebService()
{
Url = String.Empty;
Method = String.Empty;
}
public WebService(string baseUrl)
{
Url = baseUrl;
Method = String.Empty;
}
public WebService(string baseUrl, string methodName)
{
Url = baseUrl;
Method = methodName;
}
// Public API
/// <summary>
/// Adds a parameter to the WebMethod invocation.
/// </summary>
/// <param name="name">Name of the WebMethod parameter (case sensitive)</param>
/// <param name="value">Value to pass to the paramenter</param>
public void AddParameter(string name, string value)
{
Params.Add(name, value);
}
public void Invoke()
{
Invoke(Method, true);
}
/// <summary>
/// Using the base url, invokes the WebMethod with the given name
/// </summary>
/// <param name="methodName">Web Method name</param>
public void Invoke(string methodName)
{
Invoke(methodName, true);
}
/// <summary>
/// Cleans all internal data used in the last invocation, except the WebService's URL. This avoids creating a new WebService object when the URL you want to use is the same.
/// </summary>
public void CleanLastInvoke()
{
ResponseSOAP = ResultXML = null;
ResultString = Method = String.Empty;
Params = new Dictionary<string, string>();
}
#region Helper Methods
/// <summary>
/// Checks if the WebService's URL and the WebMethod's name are valid. If not, throws ArgumentNullException.
/// </summary>
/// <param name="methodName">Web Method name (optional)</param>
private void AssertCanInvoke(string methodName = "")
{
if (Url == String.Empty)
throw new ArgumentNullException("You tried to invoke a webservice without specifying the WebService's URL.");
if ((methodName == "") && (Method == String.Empty))
throw new ArgumentNullException("You tried to invoke a webservice without specifying the WebMethod.");
}
private void ExtractResult(string methodName)
{
ResultString = ResponseSOAP.ToString(SaveOptions.OmitDuplicateNamespaces);
 
//ResponseSOAP.Element("RootElement").Elements("ChildElement").Attributes("Attribute1").Select(attribute => attribute.Value).ToArray());
//// Selects just the elements with namespace https://tempuri.org/ (i.e. ignores SOAP namespace)
//XmlNamespaceManager namespMan = new XmlNamespaceManager(new NameTable());
//namespMan.AddNamespace("foo", "https://tempuri.org/");
//foreach (var item in ResponseSOAP.Elements("return"))
//{
// ResponseSOAP.Element("return");
//}
//XElement webMethodResult = ResponseSOAP.XPathSelectElement("//foo:" + methodName + "Result", namespMan);
//XElement webMethodResult = ResponseSOAP.Element("return");
//// If the result is an XML, return it and convert it to string
//if (webMethodResult.FirstNode.NodeType == XmlNodeType.Element)
//{
// ResultXML = XDocument.Parse(webMethodResult.FirstNode.ToString());
// ResultXML = Utils.RemoveNamespaces(ResultXML);
// ResultString = ResultXML.ToString();
//}
//// If the result is a string, return it and convert it to XML (creating a root node to wrap the result)
//else
//{
// ResultString = webMethodResult.FirstNode.ToString();
// ResultXML = XDocument.Parse("<root>" + ResultString + "</root>");
//}
}
/// <summary>
/// Invokes a Web Method, with its parameters encoded or not.
/// </summary>
/// <param name="methodName">Name of the web method you want to call (case sensitive)</param>
/// <param name="encode">Do you want to encode your parameters? (default: true)</param>
private void Invoke(string methodName, bool encode)
{
AssertCanInvoke(methodName);
string RequestDateTime;
string ExpiredDateTime;
DateTime TempDateTime;
TempDateTime = DateTime.UtcNow;
RequestDateTime = TempDateTime.ToString("yyyy-MM-dd") + "T" + TempDateTime.ToString("HH:mm:ss.fff")+"Z";
TempDateTime = DateTime.UtcNow.AddMinutes(5);
ExpiredDateTime = TempDateTime.ToString("yyyy-MM-dd") + "T" + TempDateTime.ToString("HH:mm:ss.fff") + "Z";
string soapStr =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""https://schemas.xmlsoap.org/soap/envelope/"">
<SOAP-ENV:Header xmlns:SOAP-ENV=""https://schemas.xmlsoap.org/soap/envelope/"">
<wsse:Security soap:mustUnderstand=""1"" xmlns:wsse=""https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:wsu=""https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"">
<wsu:Timestamp wsu:Id=""TS-C4BB21565DFD1F755B14194887813621"">";
soapStr += "<wsu:Created>" + RequestDateTime + "</wsu:Created>";
soapStr += "<wsu:Expires>" + ExpiredDateTime + "</wsu:Expires>";
soapStr +=
@"</wsu:Timestamp>
</wsse:Security>
</SOAP-ENV:Header>
<soap:Body>
<ns2:{0} xmlns:ns2=""https://services/"">
{1}
</ns2:{0}>
</soap:Body>
</soap:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
req.Headers.Add("SOAPAction", "\"https://tempuri.org/" + 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);
}
}
using (StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream()))
{
string result = responseReader.ReadToEnd();
ResponseSOAP = XDocument.Parse(Utils.UnescapeString(result));
ExtractResult(methodName);
}
}
#endregion
}
 

 

//===== Utils.class =====================

 

using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using System.Xml.Linq;
public static class Utils
{
/// <summary>
/// Remove all xmlns:* instances from the passed XmlDocument to simplify our xpath expressions
/// </summary>
public static XDocument RemoveNamespaces(XDocument oldXml)
{
// FROM: https://social.msdn.microsoft.com/Forums/en-US/bed57335-827a-4731-b6da-a7636ac29f21/xdocument-remove-namespace?forum=linqprojectgeneral
try
{
XDocument newXml = XDocument.Parse(Regex.Replace(
oldXml.ToString(),
@"(xmlns:?[^=]*=[""][^""]*[""])",
"",
RegexOptions.IgnoreCase | RegexOptions.Multiline)
);
return newXml;
}
catch (XmlException error)
{
throw new XmlException(error.Message + " at Utils.RemoveNamespaces");
}
}
/// <summary>
/// Converts a string that has been HTML-enconded for HTTP transmission into a decoded string.
/// </summary>
/// <param name="escapedString">String to decode.</param>
/// <returns>Decoded (unescaped) string.</returns>
public static string UnescapeString(string escapedString)
{
return HttpUtility.HtmlDecode(escapedString);
}
/// <summary>
/// Remove all xmlns:* instances from the passed XmlDocument to simplify our xpath expressions
/// </summary>
public static XDocument RemoveNamespaces(string oldXml)
{
XDocument newXml = XDocument.Parse(oldXml);
return RemoveNamespaces(newXml);
}
}

 

 

Posted On: 01-Jan-2015 16:23
 Log In to Chat