Code to find Response time of a site

by Vinoth 2008-10-24 16:08:26

Hi,
Following code will give response time and the location.

import java.net.*;
import java.io.*;
public class CheckURLResponse {
public static void main(String args[]) {
try{
//System.out.println("Enter a URL to check");
System.out.println("eg:"http://www.hiox.org/"");
java.io.StreamTokenizer Input=new java.io.StreamTokenizer(System.in);
System.out.println("Enter a URL to check");
Input.nextToken();
String urlString=new String(Input.sval);
HttpURLConnection.setFollowRedirects(false);
URL url = new URL(urlString);
URLConnection connection =
url.openConnection();
if(connection instanceof HttpURLConnection) {
HttpURLConnection httpConnection=(HttpURLConnection)connection;
httpConnection.connect();
int response =httpConnection.getResponseCode();
System.out.println("Response: " + response);
String location =httpConnection.getHeaderField("Location");
if(location != null){
System.out.println("Location: " + location);
}
InputStream is =httpConnection.getInputStream();
byte[] buffer = new byte [256];
while (is.read(buffer)!=-1) {}
is.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}

Tagged in:

2038
like
0
dislike
0
mail
flag

You must LOGIN to add comments