You can use java.net.HttpURLConnection and java.net.URL libraries. I use a little util class to do any http request I need in my apps. It allows for setting both connect and read timeouts.
package utility;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLReader {
/**
* @param args
*/
public static String getURL(String p_URL) {
StringBuffer out = new StringBuffer();
try {
//get default report id
URL url = new URL(p_URL);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(false);
huc.setConnectTimeout(30 * 1000);
huc.setReadTimeout(300 * 1000);
huc.setRequestMethod("GET");
huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
huc.connect();
InputStream input = huc.getInputStream();
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(input));
String inputLine;
//int x = 0;
while ((inputLine = in.readLine()) != null)
{
out.append(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return out.toString();
}
public static String getURL(String p_URL, String p_userPass) {
StringBuffer out = new StringBuffer();
try {
//get default report id
URL url = new URL(p_URL);
// Encode String
String encoding = new sun.misc.BASE64Encoder().encode (p_userPass.getBytes());
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(false);
huc.setRequestProperty("Authorization", "Basic " + encoding);
huc.setConnectTimeout(30 * 1000);
huc.setReadTimeout(300 * 1000);
huc.setRequestMethod("GET");
huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
huc.connect();
InputStream input = huc.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input, "ISO-8859-1"));
String line;
while ((line = in.readLine()) != null) {
out.append(line);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return out.toString();
}
public static InputStream getURLStream(String p_URL, String p_userPass) {
InputStream out = null;
try {
//get default report id
URL url = new URL(p_URL);
// Encode String
String encoding = new sun.misc.BASE64Encoder().encode (p_userPass.getBytes());
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(false);
huc.setRequestProperty("Authorization", "Basic " + encoding);
huc.setConnectTimeout(30 * 1000);
huc.setReadTimeout(300 * 1000);
huc.setRequestMethod("GET");
huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
huc.connect();
out = huc.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return out;
}
public static InputStream getURLStream(String p_URL) {
InputStream out = null;
try {
//get default report id
URL url = new URL(p_URL);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(false);
huc.setConnectTimeout(30 * 1000);
huc.setReadTimeout(300 * 1000);
huc.setRequestMethod("GET");
huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
huc.connect();
out = huc.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return out;
}
}