{"id":1486,"date":"2014-11-24T08:00:32","date_gmt":"2014-11-24T08:00:32","guid":{"rendered":"http:\/\/www.diogonunes.com\/blog\/?p=1486"},"modified":"2020-05-15T14:24:30","modified_gmt":"2020-05-15T13:24:30","slug":"calling-a-web-method-in-c-without-a-service-reference","status":"publish","type":"post","link":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/","title":{"rendered":"Calling a Web Method in C# without Service Reference"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?resize=506%2C350\" alt=\"This is a SOAP request\" width=\"506\" height=\"350\" class=\"aligncenter size-full wp-image-1498\" srcset=\"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?w=506&amp;ssl=1 506w, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?resize=400%2C276&amp;ssl=1 400w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/p>\n<p>Last week I gave you a method that, <a href=\"http:\/\/www.diogonunes.com\/blog\/?p=1451\">using SOAP and HttpWebRequest, allowed you to invoke a Web Method without a WSDL or a Web Reference<\/a>. Well today, I&#8217;ll give you an improved version of that method. In fact I&#8217;ll give you a whole ready-to-use class with additional functionality.<\/p>\n<p><!--more--><\/p>\n<p>Recapping, if you need to call a web method but fail to have a WSDL or can&#8217;t use a Web or Service Reference because you want your code to be dynamic, then SOAP and <code>HttpWebRequest<\/code>s are the way to go. You can then create a Proxy having the same API (methods, inputs, outputs) as the WebService you&#8217;re trying to call. For instance:<\/p>\n<pre><code>internal class ExampleAPIProxy\n{\n    private static WebService ExampleAPI = new WebService(\"http:\/\/...\/example.asmx\");    \/\/ DEFAULT location of the WebService, containing the WebMethods\n\n    public static void ChangeUrl(string webserviceEndpoint)\n    {\n        ExampleAPI = new WebService(webserviceEndpoint);\n    }\n\n    public static string ExampleWebMethod(string name, int number)\n    {\n        ExampleAPI.PreInvoke();\n\n        ExampleAPI.AddParameter(\"name\", name);                    \/\/ Case Sensitive! To avoid typos, just copy the WebMethod's signature and paste it\n        ExampleAPI.AddParameter(\"number\", number.ToString());     \/\/ all parameters are passed as strings\n        try\n        {\n            ExampleAPI.Invoke(\"ExampleWebMethod\");                \/\/ name of the WebMethod to call (Case Sentitive again!)\n        }\n        finally { ExampleAPI.PosInvoke(); }\n\n        return ExampleAPI.ResultString;                           \/\/ you can either return a string or an XML, your choice\n    }\n}\n<\/code><\/pre>\n<p>For that to work you&#8217;ll need the WebService class.<\/p>\n<pre><code>\/\/\/ &lt;summary&gt;\n\/\/\/ This class is an alternative when you can't use Service References. It allows you to invoke Web Methods on a given Web Service URL.\n\/\/\/ Based on the code from http:\/\/stackoverflow.com\/questions\/9482773\/web-service-without-adding-a-reference\n\/\/\/ &lt;\/summary&gt;\npublic class WebService\n{\n    public string Url { get; private set; }\n    public string Method { get; private set; }\n    public Dictionary&lt;string, string&gt; Params = new Dictionary&lt;string, string&gt;();\n    public XDocument ResponseSOAP = XDocument.Parse(\"&lt;root\/&gt;\");\n    public XDocument ResultXML = XDocument.Parse(\"&lt;root\/&gt;\");\n    public string ResultString = String.Empty;\n\n    private Cursor InitialCursorState;\n\n    public WebService()\n    {\n        Url = String.Empty;\n        Method = String.Empty;\n    }\n    public WebService(string baseUrl)\n    {\n        Url = baseUrl;\n        Method = String.Empty;\n    }\n    public WebService(string baseUrl, string methodName)\n    {\n        Url = baseUrl;\n        Method = methodName;\n    }\n\n    \/\/ Public API\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Adds a parameter to the WebMethod invocation.\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=\"name\"&gt;Name of the WebMethod parameter (case sensitive)&lt;\/param&gt;\n    \/\/\/ &lt;param name=\"value\"&gt;Value to pass to the paramenter&lt;\/param&gt;\n    public void AddParameter(string name, string value)\n    {\n        Params.Add(name, value);\n    }\n\n    public void Invoke()\n    {\n        Invoke(Method, true);\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Using the base url, invokes the WebMethod with the given name\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=\"methodName\"&gt;Web Method name&lt;\/param&gt;\n    public void Invoke(string methodName)\n    {\n        Invoke(methodName, true);\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Cleans all internal data used in the last invocation, except the WebService's URL.\n    \/\/\/ This avoids creating a new WebService object when the URL you want to use is the same.\n    \/\/\/ &lt;\/summary&gt;\n    public void CleanLastInvoke()\n    {\n        ResponseSOAP = ResultXML = null;\n        ResultString = Method = String.Empty;\n        Params = new Dictionary&lt;string, string&gt;();\n    }\n\n    #region Helper Methods\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Checks if the WebService's URL and the WebMethod's name are valid. If not, throws ArgumentNullException.\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=\"methodName\"&gt;Web Method name (optional)&lt;\/param&gt;\n    private void AssertCanInvoke(string methodName = \"\")\n    {\n        if (Url == String.Empty)\n            throw new ArgumentNullException(\"You tried to invoke a webservice without specifying the WebService's URL.\");\n        if ((methodName == \"\") &amp;&amp; (Method == String.Empty))\n            throw new ArgumentNullException(\"You tried to invoke a webservice without specifying the WebMethod.\");\n    }\n\n    private void ExtractResult(string methodName)\n    {\n        \/\/ Selects just the elements with namespace http:\/\/tempuri.org\/ (i.e. ignores SOAP namespace)\n        XmlNamespaceManager namespMan = new XmlNamespaceManager(new NameTable());\n        namespMan.AddNamespace(\"foo\", \"http:\/\/tempuri.org\/\");\n\n        XElement webMethodResult = ResponseSOAP.XPathSelectElement(\"\/\/foo:\" + methodName + \"Result\", namespMan);\n        \/\/ If the result is an XML, return it and convert it to string\n        if (webMethodResult.FirstNode.NodeType == XmlNodeType.Element)\n        {\n            ResultXML = XDocument.Parse(webMethodResult.FirstNode.ToString());\n            ResultXML = Utils.RemoveNamespaces(ResultXML);\n            ResultString = ResultXML.ToString();\n        }\n        \/\/ If the result is a string, return it and convert it to XML (creating a root node to wrap the result)\n        else\n        {\n            ResultString = webMethodResult.FirstNode.ToString();\n            ResultXML = XDocument.Parse(\"&lt;root&gt;\" + ResultString + \"&lt;\/root&gt;\");\n        }\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Invokes a Web Method, with its parameters encoded or not.\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=\"methodName\"&gt;Name of the web method you want to call (case sensitive)&lt;\/param&gt;\n    \/\/\/ &lt;param name=\"encode\"&gt;Do you want to encode your parameters? (default: true)&lt;\/param&gt;\n    private void Invoke(string methodName, bool encode)\n    {\n        AssertCanInvoke(methodName);\n        string soapStr =\n            @\"&lt;?xml version=\"\"1.0\"\" encoding=\"\"utf-8\"\"?&gt;\n                &lt;soap:Envelope xmlns:xsi=\"\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\"\n                   xmlns:xsd=\"\"http:\/\/www.w3.org\/2001\/XMLSchema\"\"\n                   xmlns:soap=\"\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\"\"&gt;\n                  &lt;soap:Body&gt;\n                    &lt;{0} xmlns=\"\"http:\/\/tempuri.org\/\"\"&gt;\n                      {1}\n                    &lt;\/{0}&gt;\n                  &lt;\/soap:Body&gt;\n                &lt;\/soap:Envelope&gt;\";\n\n        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);\n        req.Headers.Add(\"SOAPAction\", \"\\\"http:\/\/tempuri.org\/\" + methodName + \"\\\"\");\n        req.ContentType = \"text\/xml;charset=\\\"utf-8\\\"\";\n        req.Accept = \"text\/xml\";\n        req.Method = \"POST\";\n\n        using (Stream stm = req.GetRequestStream())\n        {\n            string postValues = \"\";\n            foreach (var param in Params)\n            {\n                if (encode) postValues += string.Format(\"&lt;{0}&gt;{1}&lt;\/{0}&gt;\", HttpUtility.HtmlEncode(param.Key), HttpUtility.HtmlEncode(param.Value));\n                else postValues += string.Format(\"&lt;{0}&gt;{1}&lt;\/{0}&gt;\", param.Key, param.Value);\n            }\n\n            soapStr = string.Format(soapStr, methodName, postValues);\n            using (StreamWriter stmw = new StreamWriter(stm))\n            {\n                stmw.Write(soapStr);\n            }\n        }\n\n        using (StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream()))\n        {\n            string result = responseReader.ReadToEnd();\n            ResponseSOAP = XDocument.Parse(Utils.UnescapeString(result));\n            ExtractResult(methodName);\n        }\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ This method should be called before each Invoke().\n    \/\/\/ &lt;\/summary&gt;\n    internal void PreInvoke()\n    {\n        CleanLastInvoke();\n        InitialCursorState = Cursor.Current;\n        Cursor.Current = Cursor.WaitCursor;\n        \/\/ feel free to add more instructions to this method\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ This method should be called after each (successful or unsuccessful) Invoke().\n    \/\/\/ &lt;\/summary&gt;\n    internal void PosInvoke()\n    {\n        Cursor.Current = InitialCursorState;\n        \/\/ feel free to add more instructions to this method\n    }\n\n    #endregion\n}\n<\/code><\/pre>\n<p>And lastly the Utils class:<\/p>\n<pre><code>public static class Utils\n{\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Remove all xmlns:* instances from the passed XmlDocument to simplify our xpath expressions\n    \/\/\/ &lt;\/summary&gt;\n    public static XDocument RemoveNamespaces(XDocument oldXml)\n    {\n        \/\/ FROM: http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/bed57335-827a-4731-b6da-a7636ac29f21\/xdocument-remove-namespace?forum=linqprojectgeneral\n        try\n        {\n            XDocument newXml = XDocument.Parse(Regex.Replace(\n                oldXml.ToString(),\n                @\"(xmlns:?[^=]*=[\"\"][^\"\"]*[\"\"])\",\n                \"\",\n                RegexOptions.IgnoreCase | RegexOptions.Multiline)\n            );\n            return newXml;\n        }\n        catch (XmlException error)\n        {\n            throw new XmlException(error.Message + \" at Utils.RemoveNamespaces\");\n        } \n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Remove all xmlns:* instances from the passed XmlDocument to simplify our xpath expressions\n    \/\/\/ &lt;\/summary&gt;\n    public static XDocument RemoveNamespaces(string oldXml)\n    {\n        XDocument newXml = XDocument.Parse(oldXml);\n        return RemoveNamespaces(newXml);\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Converts a string that has been HTML-enconded for HTTP transmission into a decoded string.\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=\"escapedString\"&gt;String to decode.&lt;\/param&gt;\n    \/\/\/ &lt;returns&gt;Decoded (unescaped) string.&lt;\/returns&gt;\n    public static string UnescapeString(string escapedString)\n    {\n        return HttpUtility.HtmlDecode(escapedString);\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Last week I gave you a method that, using SOAP and HttpWebRequest, allowed you to invoke a Web Method without a WSDL or a Web Reference. Well today, I&#8217;ll give you an improved version of that method. In fact I&#8217;ll give you a whole ready-to-use class with additional functionality.<\/p>\n","protected":false},"author":1,"featured_media":1498,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[47,29,31,48],"class_list":["post-1486","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-c-sharp","tag-coding","tag-tutorial","tag-web-dev"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Calling a Web Method in C# without Service Reference - The Geeky Gecko<\/title>\n<meta name=\"description\" content=\"If you need to call a web method but fail to have a WSDL or want your code to be dynamic, then SOAP and `HttpWebRequest`s are the way to go.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Calling a Web Method in C# without Service Reference - The Geeky Gecko\" \/>\n<meta property=\"og:description\" content=\"If you need to call a web method but fail to have a WSDL or want your code to be dynamic, then SOAP and `HttpWebRequest`s are the way to go.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/\" \/>\n<meta property=\"og:site_name\" content=\"The Geeky Gecko\" \/>\n<meta property=\"article:published_time\" content=\"2014-11-24T08:00:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-15T13:24:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"506\" \/>\n\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Diogo Nunes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@dialexnunes\" \/>\n<meta name=\"twitter:site\" content=\"@dialexnunes\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Diogo Nunes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/\"},\"author\":{\"name\":\"Diogo Nunes\",\"@id\":\"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/a6fa79b293f22912664654fcfbd2da0c\"},\"headline\":\"Calling a Web Method in C# without Service Reference\",\"datePublished\":\"2014-11-24T08:00:32+00:00\",\"dateModified\":\"2020-05-15T13:24:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/\"},\"wordCount\":139,\"publisher\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/a6fa79b293f22912664654fcfbd2da0c\"},\"image\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1\",\"keywords\":[\"c#\",\"coding\",\"tutorial\",\"web dev\"],\"articleSection\":[\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/\",\"url\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/\",\"name\":\"Calling a Web Method in C# without Service Reference - The Geeky Gecko\",\"isPartOf\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1\",\"datePublished\":\"2014-11-24T08:00:32+00:00\",\"dateModified\":\"2020-05-15T13:24:30+00:00\",\"description\":\"If you need to call a web method but fail to have a WSDL or want your code to be dynamic, then SOAP and `HttpWebRequest`s are the way to go.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1\",\"width\":506,\"height\":350},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.diogonunes.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Calling a Web Method in C# without Service Reference\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.diogonunes.com\/blog\/#website\",\"url\":\"https:\/\/www.diogonunes.com\/blog\/\",\"name\":\"The Geeky Gecko\",\"description\":\"The Geeky Gecko\",\"publisher\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/a6fa79b293f22912664654fcfbd2da0c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.diogonunes.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/a6fa79b293f22912664654fcfbd2da0c\",\"name\":\"Diogo Nunes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2026\/04\/Geeky-Gecko-2026-v2.png?fit=799%2C799&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2026\/04\/Geeky-Gecko-2026-v2.png?fit=799%2C799&ssl=1\",\"width\":799,\"height\":799,\"caption\":\"Diogo Nunes\"},\"logo\":{\"@id\":\"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/www.diogonunes.com\",\"https:\/\/x.com\/dialexnunes\"],\"url\":\"https:\/\/www.diogonunes.com\/blog\/author\/diogo-nunes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Calling a Web Method in C# without Service Reference - The Geeky Gecko","description":"If you need to call a web method but fail to have a WSDL or want your code to be dynamic, then SOAP and `HttpWebRequest`s are the way to go.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/","og_locale":"en_US","og_type":"article","og_title":"Calling a Web Method in C# without Service Reference - The Geeky Gecko","og_description":"If you need to call a web method but fail to have a WSDL or want your code to be dynamic, then SOAP and `HttpWebRequest`s are the way to go.","og_url":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/","og_site_name":"The Geeky Gecko","article_published_time":"2014-11-24T08:00:32+00:00","article_modified_time":"2020-05-15T13:24:30+00:00","og_image":[{"width":506,"height":350,"url":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1","type":"image\/png"}],"author":"Diogo Nunes","twitter_card":"summary_large_image","twitter_creator":"@dialexnunes","twitter_site":"@dialexnunes","twitter_misc":{"Written by":"Diogo Nunes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#article","isPartOf":{"@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/"},"author":{"name":"Diogo Nunes","@id":"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/a6fa79b293f22912664654fcfbd2da0c"},"headline":"Calling a Web Method in C# without Service Reference","datePublished":"2014-11-24T08:00:32+00:00","dateModified":"2020-05-15T13:24:30+00:00","mainEntityOfPage":{"@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/"},"wordCount":139,"publisher":{"@id":"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/a6fa79b293f22912664654fcfbd2da0c"},"image":{"@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1","keywords":["c#","coding","tutorial","web dev"],"articleSection":["Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/","url":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/","name":"Calling a Web Method in C# without Service Reference - The Geeky Gecko","isPartOf":{"@id":"https:\/\/www.diogonunes.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#primaryimage"},"image":{"@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1","datePublished":"2014-11-24T08:00:32+00:00","dateModified":"2020-05-15T13:24:30+00:00","description":"If you need to call a web method but fail to have a WSDL or want your code to be dynamic, then SOAP and `HttpWebRequest`s are the way to go.","breadcrumb":{"@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#primaryimage","url":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1","width":506,"height":350},{"@type":"BreadcrumbList","@id":"https:\/\/www.diogonunes.com\/blog\/calling-a-web-method-in-c-without-a-service-reference\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.diogonunes.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Calling a Web Method in C# without Service Reference"}]},{"@type":"WebSite","@id":"https:\/\/www.diogonunes.com\/blog\/#website","url":"https:\/\/www.diogonunes.com\/blog\/","name":"The Geeky Gecko","description":"The Geeky Gecko","publisher":{"@id":"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/a6fa79b293f22912664654fcfbd2da0c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.diogonunes.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/a6fa79b293f22912664654fcfbd2da0c","name":"Diogo Nunes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2026\/04\/Geeky-Gecko-2026-v2.png?fit=799%2C799&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2026\/04\/Geeky-Gecko-2026-v2.png?fit=799%2C799&ssl=1","width":799,"height":799,"caption":"Diogo Nunes"},"logo":{"@id":"https:\/\/www.diogonunes.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/www.diogonunes.com","https:\/\/x.com\/dialexnunes"],"url":"https:\/\/www.diogonunes.com\/blog\/author\/diogo-nunes\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/This-is-a-SOAP-request.png?fit=506%2C350&ssl=1","jetpack-related-posts":[{"id":1451,"url":"https:\/\/www.diogonunes.com\/blog\/calling-webservice-without-wsdl-or-web-reference\/","url_meta":{"origin":1486,"position":0},"title":"Calling Web Service without WSDL or Web Reference","author":"Diogo Nunes","date":"17 November, 2014","format":false,"excerpt":"Once I had to test in C# a dozen of web services developed by a third-party. However they provided no WSDL and no ASMX - therefore it would be impossible to use Web References. They only provided the name of the web methods, their urls, calling credentials and an XML\u2026","rel":"","context":"In &quot;Technology&quot;","block_context":{"text":"Technology","link":"https:\/\/www.diogonunes.com\/blog\/category\/tech\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/77043.jpg?fit=675%2C450&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/77043.jpg?fit=675%2C450&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/11\/77043.jpg?fit=675%2C450&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2186,"url":"https:\/\/www.diogonunes.com\/blog\/webclient-vs-httpclient-vs-httpwebrequest\/","url_meta":{"origin":1486,"position":1},"title":"WebClient vs HttpClient vs HttpWebRequest","author":"Diogo Nunes","date":"20 April, 2015","format":false,"excerpt":"Just when I was starting to get used to call WebServices through WSDL - like I showed here and here - I had to call a RESTful API. If you don't know what I'm talking about you're like me a week ago. Let's just say that: a WSDL API uses\u2026","rel":"","context":"In &quot;Technology&quot;","block_context":{"text":"Technology","link":"https:\/\/www.diogonunes.com\/blog\/category\/tech\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2015\/04\/restless.png?fit=510%2C350&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":4077,"url":"https:\/\/www.diogonunes.com\/blog\/ecosia-review\/","url_meta":{"origin":1486,"position":2},"title":"Ecosia search engine (review)","author":"Diogo Nunes","date":"6 September, 2021","format":false,"excerpt":"Plant trees while you search the web Last year I stumbled upon the search engine Ecosia. Their pitch is that every search you make contributes to plant trees across the world. On average, it takes 45 searches to plant a tree. I loved the concept, so I gave it a\u2026","rel":"","context":"In &quot;Technology&quot;","block_context":{"text":"Technology","link":"https:\/\/www.diogonunes.com\/blog\/category\/tech\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2020\/12\/michael-tuszynski-_oSipyHQ9kc-unsplash.jpg?fit=1200%2C799&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2020\/12\/michael-tuszynski-_oSipyHQ9kc-unsplash.jpg?fit=1200%2C799&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2020\/12\/michael-tuszynski-_oSipyHQ9kc-unsplash.jpg?fit=1200%2C799&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2020\/12\/michael-tuszynski-_oSipyHQ9kc-unsplash.jpg?fit=1200%2C799&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2020\/12\/michael-tuszynski-_oSipyHQ9kc-unsplash.jpg?fit=1200%2C799&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":4412,"url":"https:\/\/www.diogonunes.com\/blog\/testers-toolbox-6-dev-methodologies\/","url_meta":{"origin":1486,"position":3},"title":"Development processes (Toolbox #6)","author":"Diogo Nunes","date":"31 January, 2022","format":false,"excerpt":"tl;dr TDD, ATDD and BDD are different development practices that favour quality. \ud83c\udfc6 This post was featured in Software Testing Notes #44 Theory Neither of these methodologies are \"testing\" by itself. They are development practices that foster quality, which in turn make testing easier. TDD: (Unit) Test Driven Development Write\u2026","rel":"","context":"In &quot;Work&quot;","block_context":{"text":"Work","link":"https:\/\/www.diogonunes.com\/blog\/category\/work\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2021\/10\/annie-spratt-QckxruozjRg-unsplash.jpg?fit=1200%2C801&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2021\/10\/annie-spratt-QckxruozjRg-unsplash.jpg?fit=1200%2C801&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2021\/10\/annie-spratt-QckxruozjRg-unsplash.jpg?fit=1200%2C801&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2021\/10\/annie-spratt-QckxruozjRg-unsplash.jpg?fit=1200%2C801&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2021\/10\/annie-spratt-QckxruozjRg-unsplash.jpg?fit=1200%2C801&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":2702,"url":"https:\/\/www.diogonunes.com\/blog\/internet-button-tutorial-ide-atom-github\/","url_meta":{"origin":1486,"position":4},"title":"Internet Button: Web IDE, Atom, GitHub (tutorial #1)","author":"Diogo Nunes","date":"2 January, 2017","format":false,"excerpt":"First things first. Now that you can connect to your Photon, it's time to give it some intelligence - time to get coding! To manage that code let's create a git repo. GitHub The official documentation recommends that you fork their InternetButton repo. You don't have to, but it helps\u2026","rel":"","context":"In &quot;Technology&quot;","block_context":{"text":"Technology","link":"https:\/\/www.diogonunes.com\/blog\/category\/tech\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2016\/08\/Internet-Button-Tutorial1.jpg?fit=984%2C656&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2016\/08\/Internet-Button-Tutorial1.jpg?fit=984%2C656&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2016\/08\/Internet-Button-Tutorial1.jpg?fit=984%2C656&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2016\/08\/Internet-Button-Tutorial1.jpg?fit=984%2C656&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":263,"url":"https:\/\/www.diogonunes.com\/blog\/installing-play-framework-2-1-on-windows\/","url_meta":{"origin":1486,"position":5},"title":"Installing Play Framework 2.1 on Windows","author":"Diogo Nunes","date":"17 March, 2014","format":false,"excerpt":"The Play Framework is probably one of the few web frameworks that installs and works pretty well on Windows. I started web development with Django, but I had to learn Python and Django at the same time, and the documentation was not so good as they said it was. So\u2026","rel":"","context":"In &quot;Technology&quot;","block_context":{"text":"Technology","link":"https:\/\/www.diogonunes.com\/blog\/category\/tech\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/03\/lets-play-1.png?fit=1143%2C431&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/03\/lets-play-1.png?fit=1143%2C431&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/03\/lets-play-1.png?fit=1143%2C431&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/03\/lets-play-1.png?fit=1143%2C431&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.diogonunes.com\/blog\/wp-content\/uploads\/2014\/03\/lets-play-1.png?fit=1143%2C431&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/posts\/1486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/comments?post=1486"}],"version-history":[{"count":1,"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/posts\/1486\/revisions"}],"predecessor-version":[{"id":3824,"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/posts\/1486\/revisions\/3824"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/media\/1498"}],"wp:attachment":[{"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/media?parent=1486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/categories?post=1486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.diogonunes.com\/blog\/wp-json\/wp\/v2\/tags?post=1486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}