Click here to Skip to main content
15,664,115 members
Articles / Productivity Apps and Services / SAP
Posted 19 Feb 2016


5 bookmarked

Consuming SAP PI Web Service without WSDL

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
19 Feb 2016CPOL
This tip gives the very basic/simple implementation to interact with SAP PI webservice for authorized recipients.


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.


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"" 
<soapenv:Envelope xmlns:soapenv="""" 

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

private static HttpWebRequest CreateWebRequest()
	HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create
	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.

using (Stream stream = request.GetRequestStream())
    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();


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

Written By
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

-- There are no messages in this forum --