Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SAP

Consuming SAP PI Web Service without WSDL

3.50/5 (2 votes)
20 Feb 2016CPOL 25.2K  
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.

XML
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.

C#
private static HttpWebRequest CreateWebRequest()
{
	HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create
	(@"<code>WebServiceEndPointURL</code>");
	webRequest.Headers.Add(@"SOAP:Action");
	webRequest.ContentType = "text/xml;charset=\"utf-8\"";
	webRequest.Accept = "text/xml";
	webRequest.Method = "POST";
	string authorization = "<code>username</code>" + 
	":" + "<code>password</code>";
	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.

C#
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();
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)