Click here to Skip to main content
13,800,303 members
Click here to Skip to main content
Add your own
alternative version

Stats

12.8K views
4 bookmarked
Posted 19 Feb 2016
Licenced CPOL

Consuming SAP PI Web Service without WSDL

, 20 Feb 2016
Rate this:
Please Sign up or sign in to vote.
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
	(@"<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.

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)

Share

About the Author

Abhishek Shrotriya
Architect
Singapore Singapore
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.181214.1 | Last Updated 20 Feb 2016
Article Copyright 2016 by Abhishek Shrotriya
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid