65.9K
CodeProject is changing. Read more.
Home

Consuming SAP PI Web Service without WSDL

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (2 votes)

Feb 19, 2016

CPOL
viewsIcon

25251

This tip gives the very basic/simple implementation to interact with SAP PI webservice for authorized recipients.

Introduction

If you have a requirement to consume SAP PI webservice, here is the working and very simple approach to connect to web service without WSDL or without adding any service reference to your .NET solution.

Background

I had a requirement in one of my projects to connect to SAP PI webservice which was hosted at the client location and unable to get the WSDL relevant files/information from the client but the endpoint URL. So, using HttpWebRequest, I tried to connect with webservice and got the response defined as mentioned in the below section.

Using the Code

Create HttpWebRequest object by providing soap envelope header and create body (which needs to send to web service as an Input) of the web service POST request.

HttpWebRequest request = CreateWebRequest();
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" 
encoding=""utf-8""?>
<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" 
xmlns:inv=""http://SpecificURL"">
  <soapenv:Body>
    <AnyTag>...</AnyTag>
  </soapenv:Body>
</soapenv:Envelope>");

Create private method CreateWebRequest for populating the web request header related properties/ information.

private static HttpWebRequest CreateWebRequest()
{
	HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create
	(@"WebServiceEndPointURL");
	webRequest.Headers.Add(@"SOAP:Action");
	webRequest.ContentType = "text/xml;charset=\"utf-8\"";
	webRequest.Accept = "text/xml";
	webRequest.Method = "POST";
	string authorization = "username" + 
	":" + "password";
	byte[] binaryAuthorization = System.Text.Encoding.UTF8.GetBytes(autorization);
	authorization = Convert.ToBase64String(binaryAuthorization);
	authorization = "Basic " + authorization;
	webRequest.Headers.Add("AUTHORIZATION", authorization);
	return webRequest;
}

Use the below code to interact with the SAP PI Webservice. Catch section as mentioned in the below code will help you to identify the exact root cause of the issue which is very helpful if the web service is hosted in a different environment.

using (Stream stream = request.GetRequestStream())
{
    soapEnvelopeXml.Save(stream);
}
try
{
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (StreamReader rd = new StreamReader(response.GetResponseStream()))
        {
            string soapResult = rd.ReadToEnd();
            <here you can write the business logic implementation>
        }
    }
}
catch (Exception e)
{
    if (e is WebException && ((WebException)e).Status == WebExceptionStatus.ProtocolError)
    {
        WebResponse errResp = ((WebException)e).Response;
        using (Stream respStream = errResp.GetResponseStream())
        {
            using (StreamReader rd = new StreamReader(respStream))
            {
                string soapResult = rd.ReadToEnd();
            }
        }
    }
}