SOAP Service Request C#

Posted by user3728352 on Stack Overflow See other posts from Stack Overflow or by user3728352
Published on 2014-06-11T03:16:24Z Indexed on 2014/06/11 3:25 UTC
Read the original article Hit count: 170

Filed under:
|
|

I have this code that tries to send a request to a soap server, I'm new to soap so i am not sure if the terms i am using are correct or not please correct me I am wrong.

Basically i am accessing a web service method named getUserDomain via soap request

Here is the code:

    public void CallWebService()
    {
        var _url = "https://....com/QcXmlWebService/QcXmlWebService.asmx?wsdl";
        var _action = "https://....com/QcXmlWebService/QcXmlWebService.asmx?op=GetUserDomains";

        XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
        HttpWebRequest webRequest = CreateWebRequest(_url, _action);
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
        webRequest.BeginGetResponse(null, null);
        // 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.
        asyncResult.AsyncWaitHandle.WaitOne();

        // 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();
            }
            Console.Write(soapResult);
        }
    }

    private 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 XmlDocument CreateSoapEnvelope()
    {
        XmlDocument soapEnvelop = new XmlDocument();
         string oRequest = "";
        oRequest = @"<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:qcx=""http://smething.com/QCXML"">";
        oRequest = oRequest + "<soap:Header/>";
        oRequest = oRequest + "<soap:Body>";
        oRequest = oRequest + "<qcx:GetUserDomains>";
        oRequest = oRequest + "<qcx:inputXml><![CDATA[";
        oRequest = oRequest + "<GetUserDomains>";
        oRequest = oRequest + "<login>";
        oRequest = oRequest + "<domain_name>MBB_BTS</domain_name>";
        oRequest = oRequest + "<project_name>WCDMA_BTS_IV</project_name>";
        oRequest = oRequest + "<user_name>user</user_name>";
        oRequest = oRequest + "<password>pass</password>";
        oRequest = oRequest + "</login>";
        oRequest = oRequest + "</GetUserDomains>";
        oRequest = oRequest + " ]]>";
        oRequest = oRequest + "</qcx:inputXml>";
        oRequest = oRequest + "</qcx:GetUserDomains>";
        oRequest = oRequest + "</soap:Body>";
        oRequest = oRequest + "</soap:Envelope>";
        soapEnvelop.LoadXml(oRequest);
        return soapEnvelop;
    }

    private void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }

This code i have seen somewhere in stack overflow before as an answer but i couldn't get it to work... The error im getting is threw exception System.net.webexception. the remote server returned an error :(500) internal server

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about soap