Show TOC Start of Content Area

This graphic is explained in the accompanying text Get Request Using Proxy   Locate the document in its SAP Library structure

 

The following example shows a GET request trough specified proxy to a specified host and port, and prints the response:

import com.sap.httpclient.HttpClient;

import com.sap.httpclient.HostConfiguration;

import com.sap.httpclient.http.methods.GET;

 

/**

 * This example makes a get request trough specified proxy to a specified host &

 * port, and prints the response

 *

 */

public class SimpleExample2 {

   public static void main(String[] args) {

      try {

         // getting the proxy host and port and the http host and port from

         // the arguments

         String host = args[0];

         int port = Integer.parseInt(args[1]);

         String proxyHost = args[2];

         int proxyPort = Integer.parseInt(args[3]);

 

         // creating the host configuration

         HostConfiguration hostConfig = new HostConfiguration(host, port);

 

         // setting the proxy to the host configuration

         hostConfig.setProxy(proxyHost, proxyPort);

 

         // creating the http client instance

         HttpClient client = new HttpClient();

 

         // setting the host configuration to the client

         client.setHostConfiguration(hostConfig);

 

         // creating the get method instance

         GET httpGet = new GET();

 

         try {

            // executing the method

            client.executeMethod(httpGet);

            // showing the response

            System.out.println("RESPONSE  : ");

            System.out.println(httpGet.getResponseBodyAsString());

         } finally { // we will not use the connection any

            // more so we release it

            httpGet.releaseConnection();

         }

      } catch (Exception e) {

         e.printStackTrace();

         System.out.println("USSAGE : java SimpleExample1 "

               + "<http host> <http port> " + "<proxy host> <proxy port>");

      }

   }

}

 

End of Content Area