
The following example shows a direct GET request 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 direct get request to a specified host & port, and
* prints the response
*
*/
public class SimpleExample1 {
public static void main(String[] args) {
try {
// getting the host and port from the arguments
String host = args[0];
int port = Integer.parseInt(args[1]);
// creating the host configuration
HostConfiguration hostConfig = new HostConfiguration(host, port);
// 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>");
}
}
}