One way it to use the Java HTTP methods:
public static void main(String[] args) {
try {
URL x = new URL("http://www.google.com");
URLConnection y = x.openConnection();
InputStream in = y.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String s;
do {
s = br.readLine();
if (s != null) {
System.out.println(s);
}
} while (s != null);
} catch (Exception e) {
e.printStackTrace();
}
}
You could also use the Apache Commons Httpclient:
public void servletImplementation() {
try {
URI url = new URI("http://www.google.com", false);
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
HttpMethod method = new GetMethod();
method.setURI(url);
client.executeMethod(method);
if (method.getStatusCode() > 399) {
throw new HttpException(method.getStatusLine().toString());
}
Header[] headers = method.getResponseHeaders();
for (Header header : headers) {
System.out.println(header.getName() + ":" + header.getValue());
}
String responseBody = method.getResponseBodyAsString();
System.out.println(responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}