call a SOAP web service without using the WSDL or proxy classes RRS feed

  • Question

  • User580518845 posted

    I need to call a SOAP web service in .NET 4.0 C# without using the WSDL or proxy classes

    here is the link I used as the reference.

    But when I reach request.GetResponse(), I get error."The remote server returned an error: (500) Internal Server Error."

    using (Stream stream = request.GetRequestStream())

    using (WebResponse response = request.GetResponse())

    My request header is created like this

    public HttpWebRequest CreateWebRequest()
    string soapAction =@"SOAP:Action"; // is this correct?
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://myServiceURL/services");
    // webRequest.Headers.Add(@"SOAP:Action");
    //webRequest.ContentType = "text/xml;charset=\"utf-8\"";
    webRequest.ContentType = "text/xml;charset=\"utf-8\";action=\""+ soapAction + "\"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    return webRequest;

    Any help would be appreciated.

    Saturday, June 15, 2013 6:33 PM

All replies

  • User1690870723 posted

    I found this example:

    public static void CallWebService()
                var _url = "http://xxxxxxxxx/Service1.asmx";
                var _action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";
                XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
                HttpWebRequest webRequest = CreateWebRequest(_url, _action);
                InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
                // begin async call to web request.
                IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
                // suspend this thread until call is complete. You might want to
                // do something usefull here like update your UI.
                // get the response from the completed web request.
                string soapResult;
                using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                        soapResult = rd.ReadToEnd();
            private static HttpWebRequest CreateWebRequest(string url, string action)
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Headers.Add("SOAPAction", action);
                webRequest.ContentType = "text/xml;charset=\"utf-8\"";
                webRequest.Accept = "text/xml";
                webRequest.Method = "POST";
                return webRequest;
            private static XmlDocument CreateSoapEnvelope()
                XmlDocument soapEnvelop = new XmlDocument();
                soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><HelloWorld xmlns=""http://tempuri.org/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><int1 xsi:type=""xsd:integer"">12</int1><int2 xsi:type=""xsd:integer"">32</int2></HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>");
                return soapEnvelop;
            private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
                using (Stream stream = webRequest.GetRequestStream())


    Hope it helps.

    Monday, June 17, 2013 9:40 AM
  • User580518845 posted

    Specs ,I just saw your reply when I was ready to post my finding.Thank you very much for your time and effort.Really appreciate it!

    finally got it working.
    Here is the code, It may help someone!

    Public Function CreateWebRequest(ASMXURL as string) As HttpWebRequest
    Dim soapAction As String = Chr(34) & "http://voa.n/Service/Inventory" & Chr(34) 'get soapAction from WSDL, May need toadd one extra (chr(34) for Java service
    Dim webRequest2 As HttpWebRequest = DirectCast(WebRequest.Create(ASMXURL), HttpWebRequest)
    webRequest2.Headers.Add("SOAPAction", soapAction)
    webRequest2.Headers.Add("Accept-Encoding", "gzip,deflate") ' got it from SOAPUI program (you can downloadit, add your xml to verify the request --DO this FIRST--
    webRequest2.ContentType = "text/xml;charset=UTF-8"
    webRequest2.Method = "POST"
    'Then just follow this link
    ' Reference: http://www.roelvanlisdonk.nl/?p=1893  or you may  try Spces code above.
    End Function

    Monday, June 17, 2013 4:59 PM
  • User-1780339606 posted

    in my case supplying soap action with header does not work.

    However I can set it through ContentType 

    webRequest.ContentType = string.Format("application/soap+xml;charset=UTF-8;action=\"{0}\"", action);

    please note that action is a name from wsdl file. So if wsdl defines it as 

    <operation name="UserDefinedOperation">
    <soap12:operation soapAction="urn:#UserDefinedOperation" soapActionRequired="true"/>

    then action parameter has to be soapAction attribute value from wsdl file 

    Monday, May 30, 2016 9:40 AM