Skip to Content
Technical Articles
Author's profile photo Khusal Shah

Easiest Way of calling a SAP PI web-service using JAVA(OKHTTP)

Update 27th Feb 2020 : 

The previous link to the jar was not working or did not have the necessary classes to execute the given function below.

This is why I have shared the jars in one Gdrive link – OKHTTP GDrive


Hello SAP PI/PO Consultants,

In this blog post we will try to explore how can you easily trigger a web service already provided by SAP PI or a SOAP sender interface externally using JAVA (OKHTTP).

I am really fascinated by how we can develop external tools that can be used by our customers instead of them login into the system themselves and need to learn each and every functionality of SAP PI.

SAP PI provides various prepackage web services for different reasons. These can be accessed using WSNavigator of SAP PI.

Few of the web services which I have explored myself are:

Adapter Message Monitoring – used to read PI messages
IChannelAdmin – Start/Stop Channel

I will highly recommend you to read the following blogs before understanding the actual JAVA code

Adapter Message Monitoring

Channel Admin Service

Now that you have understood we have these prepackage web services that we can use.

Let me jump you to the actual purpose of the blog.

I was looking to make a JAVA based GUI tool where you can start/stop channel externally which would be useful during cutover activities on your SAP Landscape without a need for an SAP PI consultant to do it for you.

For that, I have to trigger a URL after the channels were selected by the user.

If you have read the previous blog shared, you must be well informed by now that I will have to make a SOAP call to the URL using a POST method.

Now there are many ways in which one can do this in JAVA. But I will be sharing the by far easiest of ways which one can use.

The key is to use an external JAR – OKHTTP

This jar will do the magic with most minimal steps,

Now let us check how?

below is the SOAP Invoke Call function.

The above functions take 4 parameters :

URL – URL of the SOAP web service.
REQUEST STRING – SOAP envelope used as input ( this can be easily taken from external tools like SOAPUI or POSTMAN)
USERNAME – SAP PI user having a role to execute web calls.
PASSWORD – SAP PI user password.

The rest of the code is pretty self-explanatory. I have also put comments for better understanding.

A few other things which I would like to highlight are from which library/package are each of the classes used to avoid confusion.

MediaType – okhttp3
OkHttpClient – okhttp3
Credentials – okhttp3
RequestBody – okhttp3

Request – okhttp3
Response – okhttp3

public String invokeMsgCall(String url,String requestString,String userName ,String password){

// Defining the MediaType we are going to use
MediaType SOAP = MediaType.get("text/xml");

// creating HTTP Client
okhttp3.OkHttpClient httpClient = new
SECONDS).readTimeout(120, TimeUnit.SECONDS).build();

// creating credentials
String cred = Credentials.basic(userName, password);

// create body with headers as shown below
RequestBody body = RequestBody.create(SOAP, requestString.getBytes());
Request req = new Request.Builder().url(url)
try {
// execute the call
Response res=httpClient.newCall(req).execute();
// return string reponse
ResponseBody resbody = res.body();
String resString = new String(resbody.bytes());
return resString;

} catch (Exception e) {
// TODO Auto-generated catch block
// Handle Expection as per your needs
return "NA";


Want to explore more on OKHTTP-

Click Here

Additional Tip:

You might be tempted to use these 3rd party jars in UDFs also.

Below blog will help you how to use these jars in UDFs

Add External Jars Files in UDF

This way you can trigger in fact any Webservice using OKHTTP3 Jar without having to worry much about SOAP Header and SOAP Envelope. This is as good as testing your Webservice using SOAPUI or POSTMAN.

Note: This is the best-known method according to my findings. If you find any better method please let me know in the comment section below. I would happily like to explore that too.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Nikiforos Chouzouris
      Nikiforos Chouzouris

      UPDATE: figured it out. Was missing the kotlin dependencies (here). Works great 🙂



      Hi Khusal,


      interesting blog. Would love to make this work, as I have been looking for an easy way to call a SAP PI Service.

      Sadly I haven’t been able to make it work yet, so I was hoping I could get some help.

      I have downloaded and imported the okhttp and the okio (downloaded from here) jar files.

      When executing the code I get following exception:

      Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/TypeCastException
      	at okhttp3.MediaType.<clinit>(MediaType.kt)
      Caused by: java.lang.ClassNotFoundException: kotlin.TypeCastException
      	at java.lang.ClassLoader.loadClass(
      	at sun.misc.Launcher$AppClassLoader.loadClass(
      	at java.lang.ClassLoader.loadClass(
      	... 3 more

      Do you have any idea what I might change/try in order to get this working?

      Thank you for your help ?

      Greetings, Nick


      Author's profile photo Khusal Shah
      Khusal Shah
      Blog Post Author

      Hi Nikiforous,

      Thanks for letting me know. I will update the jar link.

      I am glad you were able to fix the code yourself.

      This is a great step ahead.

      If you are developing some good external tool please let me and comminity know as it will be benefitial.



      Author's profile photo Nirmal Mehta
      Nirmal Mehta

      Hi Kushal, this blog is very helpful. Is there is any web service  where we can read component details on basis of Configuration Scenarios?

      Author's profile photo Khusal Shah
      Khusal Shah
      Blog Post Author

      Hello Nirmal Mehta


      Yes there is a webservice ,

      Above youtube video will help you..




      Author's profile photo Nirmal Mehta
      Nirmal Mehta

      Thanks , That worked for me 🙂

      Author's profile photo Frantisek Vojtek
      Frantisek Vojtek

      Hi Khusal,

      are attachments supported?




      Author's profile photo Khusal Shah
      Khusal Shah
      Blog Post Author

      Hi Frantisek,

      I think it should support attachment.

      Please check here for updates.