Thursday, April 2, 2015

Send GET HTTP Request over Java

To send an HTTP request over Java via GET the following code example can be used:



  HttpURLConnection connection = null;
  PrintWriter out;
BufferedReader in;
String inputLine;
String pageText = "";

try {

connection = (HttpURLConnection) gateway.openConnection();
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setDefaultUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setUseCaches(false);
connection.setRequestMethod("GET");

if (contentType != null) {
connection.setRequestProperty("CONTENT-TYPE", contentType);
}

in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));

while ((inputLine = in.readLine()) != null) {
pageText = pageText + inputLine;
}

log.info(pageText);

} catch (Exception e) {
log.error(e)
  }


No comments:

Post a Comment