How to call a web service dynamically without add service reference over SSL
<p>I'm developing a client that call a web service without add service reference. The method receives as a parameter an XML.</p>
<pre><code>static void Main(string[] args)
    {
        string xmlAux = string.Empty;
        string link = &quot;https://test.xxx.com/xxx/xxx.asmx&quot;;            
        
        string arg1 = @&quot;&lt;tuca&gt; &lt;hd&gt;  &lt;t_doc&gt;10&lt;/t_doc&gt; &lt;id_user&gt;xxx&lt;/id_user&gt; &lt;pwd&gt;xxx&lt;/pwd&gt; &lt;id_pais&gt;xxx&lt;/id_pais&gt; &lt;/hd&gt; &lt;parametros&gt; &lt;par tipo=&quot;&quot;tipo_sujeto&quot;&quot;&gt;fisico&lt;/par&gt; &lt;par tipo=&quot;&quot;calificacion&quot;&quot;&gt;xxx&lt;/par&gt; &lt;par tipo=&quot;&quot;CEDULA DE IDENTIDAD&quot;&quot;&gt;xxx&lt;/par&gt; &lt;/parametros&gt; &lt;/tuca&gt;&quot;;

        object[] arguments = { arg1 };
        
        var ws = CallWebService(link, &quot;xxx&quot;, &quot;Reporte&quot;, arguments);
        
        if (ws != null)
        {
            xmlAux = ws.ToString();
        }
    }
     [System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.Demand, Unrestricted = true)]
    internal static object CallWebService(string webServiceAsmxUrl, string serviceName, string methodName, object[] args)
    {
        System.Net.WebClient client = new System.Net.WebClient();

        // Connect To the web service
        System.IO.Stream stream = client.OpenRead(webServiceAsmxUrl + &quot;?wsdl&quot;);

        // Now read the WSDL file describing a service.
        var description = System.Web.Services.Description.ServiceDescription.Read(stream);

        ///// LOAD THE DOM /////////
        // Initialize a service description importer.
        var importer = new System.Web.Services.Description.ServiceDescriptionImporter();

        importer.ProtocolName = &quot;Soap12&quot;; // Use SOAP 1.2.

        importer.AddServiceDescription(description, null, null);

        // Generate a proxy client.
        importer.Style = System.Web.Services.Description.ServiceDescriptionImportStyle.Client;

        // Generate properties to represent primitive values.
        importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

        // Initialize a Code-DOM tree into which we will import the service.
        var nmspace = new System.CodeDom.CodeNamespace();

        var unit1 = new System.CodeDom.CodeCompileUnit();
        unit1.Namespaces.Add(nmspace);

        // Import the service into the Code-DOM tree. This creates proxy code that uses the service.
        var warning = importer.Import(nmspace, unit1);

        if (warning == 0) // If zero then we are good to go
        {
            // Generate the proxy code
            var provider1 = System.CodeDom.Compiler.CodeDomProvider.CreateProvider(&quot;CSharp&quot;);

            // Compile the assembly proxy with the appropriate references
            string[] assemblyReferences = new string[5] { &quot;System.dll&quot;, &quot;System.Web.Services.dll&quot;, &quot;System.Web.dll&quot;, &quot;System.Xml.dll&quot;, &quot;System.Data.dll&quot; };

            var parms = new System.CodeDom.Compiler.CompilerParameters(assemblyReferences);

            var results = provider1.CompileAssemblyFromDom(parms, unit1);

            // Check For Errors
            if (results.Errors.Count &gt; 0)
            {
                foreach (System.CodeDom.Compiler.CompilerError oops in results.Errors)
                {
                    System.Diagnostics.Debug.WriteLine(&quot;========Compiler error============&quot;);
                    System.Diagnostics.Debug.WriteLine(oops.ErrorText);
                }

                throw new System.Exception(&quot;Compile Error Occured calling webservice. Check Debug ouput window.&quot;);

            }

            // Finally, Invoke the web service method
            object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);

            var mi = wsvcClass.GetType().GetMethod(methodName);

            return mi.Invoke(wsvcClass, args);

        }

        else
        {
            return null;
        }

    }
</code></pre>
<p>In the line:</p>
<pre><code>return mi.Invoke(wsvcClass, args) 
</code></pre>
<p>throws exception:</p>
<blockquote>
<p>InnerException: {&quot;The underlying connection was closed: An unexpected error occurred on a receive.&quot;}</p>
<p>Message: Exception has been thrown by the target of an invocation.</p>
</blockquote>