locked
Consume SOAP and sending response back as Rest in C# RRS feed

  • Question

  • User-1419011324 posted

    I am working with .net 4.5 C# project. I am consuming SOAP service and then response back them to with REST with SharePoint 2013.

    But the issue is part of SOAP response is not parsing as XML but as string. I don't know what I am doing wrong.

    Screen marked part parsing as string not XML

    Code for Consuming SOAP

    public HttpWebRequest CreateWebRequest(string URL)
    {
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"" + URL);
    webRequest.Headers.Add(@"SOAP:Action");
    webRequest.ContentType = "text/xml;charset=\"utf-8\"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    return webRequest;
    }
    
    public string ConsumeSOAP(string XML, string URL)
    {
    HttpWebRequest request = CreateWebRequest(URL);
    XmlDocument soapEnvelopeXml = new XmlDocument();
    soapEnvelopeXml.LoadXml(@"" + XML);
    
    using (Stream stream = request.GetRequestStream())
    {
    soapEnvelopeXml.Save(stream);
    }
    using (WebResponse response = request.GetResponse())
    {
    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
    {
    string soapResult = rd.ReadToEnd();
    return soapResult;
    }
    }
    }



    Code for SOAP response to REST

    public string doLogin(string memberId, string password)
    {
    string XML = null;
    try
    {
    SOAPService soap = new SOAPService();
    XNamespace ns = @"http://schemas.xmlsoap.org/soap/envelope/";
    
    string URL = URL;
    string User = User;
    string Password = Password;
    
    string authenticateXML = string.Format(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:web=""http://webservices.xxx.xxx.com/"">
    <soapenv:Header>
    <web:header>
    <passWord>{0}</passWord>
    <userName>{1}</userName>
    </web:header>
    </soapenv:Header>
    <soapenv:Body>
    <web:authenticate>
    <authenticateRequest>
    <password>{2}</password>
    <username>{3}</username>
    </authenticateRequest>
    </web:authenticate>
    </soapenv:Body>
    </soapenv:Envelope>", Password, User, password, memberId);
    
    string authenticateResponse = soap.ConsumeSOAP(authenticateXML, URL);
    var authenticateResponseValue = XDocument.Parse(authenticateResponse);
    XML = authenticateResponseValue.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body").First().FirstNode.ToString();
    }
    catch 
    {
    
    }
    return XML;
    }



    Interface for the REST

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/doLogin?memberId={memberId}&password={password}")]
    string doLogin(string memberId, string password);




    [1]: https://i.stack.imgur.com/na54O.png

    Friday, April 28, 2017 5:54 AM

All replies

  • User475983607 posted

    If I understand correctly.  you are building a REST-SOAP adapter service.  Rather than trying to parse SOAP directly, simply add a Web Reference (to the SOAP service) using Visual Studio.  This will generate code that does the request/response SOAP parsing automatically.

    https://msdn.microsoft.com/en-us/library/tydxdyw9(v=vs.100).aspx

    https://msdn.microsoft.com/en-us/library/d9w023sx(v=vs.100).aspx

    Friday, April 28, 2017 10:32 AM
  • User-1419011324 posted

    Well I solved my issue by changing doLogin function return from string to XElement. Then store FirstNode in XNode variable and than parse that XNode to XElement. 

    Cheers 

    Friday, April 28, 2017 10:38 AM
  • User1771544211 posted

    Hi Milind Saraswala,

    Thanks for sharing your solution. Please mark your reply as answer. It will help others who encountered a similar problem to quickly find the solution.

    Jean

    Monday, May 1, 2017 7:25 AM