How to get HTTP response for a URL in Java?


log4j: Threshold =”null”.
log4j: Level value for root is [info].
log4j: root level set to INFO
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [maxFileSize] to [10000KB].
log4j: Setting property [maxBackupIndex] to [3].
log4j: Parsing layout of class: “org.apache.log4j.PatternLayout”
log4j: Setting property [conversionPattern] to [%d [%d{yyyy-MM-dd HH:mm:ss:SSS Z}] [%t] %-5p %c %L – %m %n].
log4j: setFile ended
log4j: Adding appender named [] to category [root].
Response–>STATUS 200


import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpResponse;
import org.apache.log4j.Logger;

public class HttpResponseCheck {
private static final Logger logger = HttpResponseCheckLogger.getLogger(HttpResponseCheck.class);

public static void main(String[] args) throws Exception {
HttpClient client = new DefaultHttpClient();
// args[0] =http://hostname:port/xyz/zbc
HttpGet request1 = new HttpGet(args[0]);
HttpResponse response1 = client.execute(request1);
try (BufferedReader br = new BufferedReader(new InputStreamReader((response1.getEntity().getContent())));) {
// Read in all of the post results into a String.
String output = “”;
Boolean keepGoing = true;
while (keepGoing) {
String currentLine = br.readLine();
if (currentLine == null) {
keepGoing = false;
} else {
output += currentLine;
System.out.println(“Response–>” + output);

catch (Exception e) {
System.out.println(“Exception” + e);




One thought on “How to get HTTP response for a URL in Java?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s