Skip to end of metadata
Go to start of metadata

Prerequisites: At least a j2sdk 1.4 version. 

Sample Java Code:

import java.net.*;
import java.io.*;
import java.util.*;

class XIHttpClient
{
 public XIHttpClient() { }

 public byte[] read2array(String file) throws Exception
 {
  InputStream in = null;
  byte[] out = new byte[0];
  try
  {
   in = new BufferedInputStream(new FileInputStream(file));
   int bufLen = 1000*1024;
   byte[] buf = new byte[bufLen];
   byte[] tmp = null;
   int len = 0;
   while((len = in.read(buf,0,bufLen)) != -1)
   {
    tmp = new byte[out.length + len];
    System.arraycopy(out,0,tmp,0,out.length);
    System.arraycopy(buf,0,tmp,out.length,len);
    out = tmp;
    tmp = null;
   }
  }
  finally
  {
   if (in != null)
    try{ in.close();}
   catch (Exception e){}
  }
  return out;
 }

 public static void main(String[] args)
 {
  XIHttpClient hc = new XIHttpClient();
  try
  {
   BufferedWriter bw = null;
   InputStream propsIn = null;
   Properties props = new Properties();
   propsIn = new FileInputStream(new File("MIDetails.txt"));
   props.load(propsIn);
   propsIn.close();

   byte[] b = hc.read2array(args[0]); //Read the request payload
   String data = new String(b);
   //URL url = new URL("http://<XISERVERHOSTNAME>:<80XX>/sap/xi/adapter_plain?namespace=<SenderNameSpace>&interface=<SenderInterfaceName>&service=<SenderBusinessSystem>&party=&agency=&scheme=&QOS=BE&sap-user=<USERNAME>&sap-password=<PASSWORD>&sap-client=<CLIENTNUMBER>&sap-language=EN");
   URL url = new URL("http://" + props.getProperty("XIServerHost") + props.getProperty("Port") + props.getProperty("Path") + props.getProperty("NameSpace") + props.getProperty("SenderMessageInterface") + props.getProperty("BusinessSystem") + props.getProperty("Party") + props.getProperty("QualityOfService") + props.getProperty("UserName") + props.getProperty("Password") + props.getProperty("SAPClient") + props.getProperty("Language") );

   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setDoInput(true);
   conn.setDoOutput(true);

   OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
   wr.write(data);
   wr.flush();

   // Read the response.
   BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
   System.out.println("HTTP Status-Code : " + conn.getResponseCode() );

   String line;
   while ((line = rd.readLine()) != null)
   {
    bw = new BufferedWriter(new FileWriter("ResponsePayload.xml"));
    bw.write(line);
    System.out.println(line);
   }

   if(bw != null)
    bw.close();

   wr.close();
   rd.close();
  }
  catch (Exception e)
  {
   System.out.println("Exception : " + e);
  }
 }
}

Properties File

Put the following properties in a file named "MIDetails.txt" and modify the red colored XXX things according to your scenario. Make sure that this file resides in a directory same as above java file.

XIServerHost=XXXX

Port=:XXXX

Path=/sap/xi/adapter_plain?

NameSpace=namespace=XXXXXXXXXXXX

SenderMessageInterface=&interface=XXXXXXXXXXX

BusinessSystem=&service=XXXXXX

Party=&party=

QualityOfService=&QOS=XX

UserName=&sap-user=XXXXXXXXX

Password=&sap-password=XXXXXXXX

SAPClient=&sap-client=XXX    

Language=&sap-language=XX

Sample Input Payload File

Copy the sample request payload of your scenario into a text or XML file (say RequestPayload.xml) which should reside in a directory same as above java file.

Compilation & Execution in Windows OS

Let's assume that the above files are in a directory say C:\test

Open a DOS prompt and change the current directory to "C:\test".

Make sure that the PATH variable has at least a java SDK path. For this type command PATH in DOS prompt. It will display all the current paths in the default environment. If the path doesn't contain a java binary path then set the path by typing the command SET PATH=.;C:\j2sdk1.4.2_XX\bin. In the above command you need to specify an existing java binary path in your system.

C:\test>SET PATH=.;C:\j2sdk1.4.2_XX\bin

Now let's compile the java code. Use command javac XIHttpClient.java

C:\test>javac XIHttpClient.java

Run the java http client using command: java XIHttpClient  RequestPayload.xml

C:\test>java XIHttpClient RequestPayload.xml