Java Utililty Methods URL Read

List of utility methods to do URL Read

Description

The list of methods to do URL Read are organized into topic(s).

Method

StringgetURL(final URL url)
get URL
final HttpURLConnection conn = connect(url, false);
final StringBuilder content = new StringBuilder();
final InputStreamReader in = new InputStreamReader(conn.getInputStream(), "UTF-8");
final char[] buff = new char[1024];
while (true) {
    final int blen = in.read(buff);
    if (blen < 0) {
        break;
...
StringgetURL(String url)
get URL
StringBuffer response = null;
try {
    URL u = new URL(url);
    HttpURLConnection httpConnection = (HttpURLConnection) u.openConnection();
    httpConnection.setUseCaches(false);
    httpConnection.setDoOutput(true);
    httpConnection.setRequestMethod("GET");
    httpConnection.connect();
...
StringgetURL(URL url, String params)
Get a GET String response
StringBuilder sb = new StringBuilder();
String lineSepatator = null;
try {
    lineSepatator = System.getProperty("line.separator");
} catch (Exception e) {
    lineSepatator = "\n";
try {
...
StringgetUrlContent(String url)
If the response code is 200, returns the content at the URL.
try {
    HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
    int responseCode = urlConnection.getResponseCode();
    if (responseCode == 200) {
        return CharStreams.toString(new InputStreamReader(urlConnection.getInputStream()));
} catch (IOException e) {
return null;
voidgetURLContent_old(final String uri, final StringBuffer content)
get URL Contenold
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
byte[] buffer = new byte[300];
int n = buffer.length;
while (n > 0) {
    n = in.read(buffer);
...
StringgetUrlContentWithRetries(String url, long timeoutMs, long retryDelayMs)
If the response code is 200, returns the content at the URL.
return getUrlContentWithRetries(url, timeoutMs, retryDelayMs, false);
URLgetUrlFollowingRedirects(String possibleRedirectionUrl)
get Url Following Redirects
URL url = new URL(possibleRedirectionUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int status = conn.getResponseCode();
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM
        || status == HttpURLConnection.HTTP_SEE_OTHER) {
    url = new URL(conn.getHeaderField("Location"));
return url;
...
String[]getUrlInfos(String urlAsString, int timeout)
get Url Infos
try {
    URL url = new URL(urlAsString);
    HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
    hConn.setRequestProperty("User-Agent", "Mozilla/5.0 Gecko/20100915 Firefox/3.6.10");
    hConn.setConnectTimeout(timeout);
    hConn.setReadTimeout(timeout);
    byte[] arr = new byte[4096];
    BufferedInputStream in = new BufferedInputStream(hConn.getInputStream(), arr.length);
...
ArrayListgetUrlSource(String url)
get Url Source
ArrayList<String> returnList = new ArrayList<String>();
returnList.clear();
URL site = new URL(url);
HttpURLConnection yc = (HttpURLConnection) site.openConnection();
yc.addRequestProperty("User-Agent", getRandomUserAgent());
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine;
while ((inputLine = in.readLine()) != null) {
...
intgetUrlStatus(String url)
Get the http status of url
HttpURLConnection conn = null;
try {
    URL u = new URL(url);
    conn = (HttpURLConnection) u.openConnection();
    conn.connect();
    return conn.getResponseCode();
} catch (MalformedURLException e) {
    e.printStackTrace();
...