Quantcast
Channel: WCF, ASMX and other Web Services
Viewing all articles
Browse latest Browse all 555

Consume SOAP and sending response back as Rest in C#

$
0
0

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


Viewing all articles
Browse latest Browse all 555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>