How do I perform HTTP GET method?
Category: commons.httpclient, viewed: 7010 time(s).
Below is an example using Commons HttpClient library to retrieve information from a website using HTTP GET method.
package org.kodejava.example.commons.httpclient;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import java.io.IOException;
public class HttpGetDemo {
public static void main(String[] args) {
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("http://www.kodejava.org");
try {
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
String response = method.getResponseBodyAsString();
System.out.println("Response = " + response);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
}
}
}
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!