public static String downloadString(URL url,Proxy proxy,int readTimeout,int socketTimeout,String encoding){
  HttpURLConnection connection=null;
  InputStream inputStream=null;
  try {
    connection=(HttpURLConnection)(proxy == null ? url.openConnection() : url.openConnection(proxy));
    connection.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36");
    connection.setReadTimeout(readTimeout);
    connection.setConnectTimeout(socketTimeout);
    connection.connect();
    if (connection.getResponseCode() >= 400) {
      throw new IOException("Response status is " + connection.getResponseCode());
    }
    if (connection.getResponseCode() >= 301) {
      String location=connection.getHeaderField("Location");
      return downloadString(new URL(location),proxy,readTimeout,socketTimeout,encoding);
    }
    if (connection.getResponseCode() == 204) {
      return StringUtils.EMPTY;
    }
    inputStream=connection.getInputStream();
    return IOUtils.toString(inputStream,encoding);
  }
 catch (  IOException ex) {
    if (LOG.isDebugEnabled()) {
      LOG.warn("Error downloading string from {}:\r\n",url,ex);
    }
 else {
      LOG.warn("Cannot download string from {}: {}",url,ex.getMessage());
    }
    return null;
  }
 finally {
    IOUtils.closeQuietly(inputStream);
    if (connection != null) {
      connection.disconnect();
    }
  }
}

public static String downloadString ( URL url , Proxy proxy , int readTimeout , int socketTimeout , String encoding ) { HttpURLConnection connection = null ; InputStream inputStream = null ; try { connection = ( HttpURLConnection ) ( proxy == null ? url . openConnection ( ) : url . openConnection ( proxy ) ) ; connection . setRequestProperty ( "User-Agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36" ) ; connection . setReadTimeout ( readTimeout ) ; connection . setConnectTimeout ( socketTimeout ) ; connection . connect ( ) ; if ( connection . getResponseCode ( ) >= 400 ) { throw new IOException ( "Response status is " + connection . getResponseCode ( ) ) ; } if ( connection . getResponseCode ( ) >= 301 ) { String location = connection . getHeaderField ( "Location" ) ; return downloadString ( new URL ( location ) , proxy , readTimeout , socketTimeout , encoding ) ; } if ( connection . getResponseCode ( ) == 204 ) { return StringUtils . EMPTY ; } inputStream = connection . getInputStream ( ) ; return IOUtils . toString ( inputStream , encoding ) ; } catch ( IOException ex ) { if ( LOG . isDebugEnabled ( ) ) { LOG . warn ( "Error downloading string from {}:\r\n" , url , ex ) ; } else { LOG . warn ( "Cannot download string from {}: {}" , url , ex . getMessage ( ) ) ; } return null ; } finally { IOUtils . closeQuietly ( inputStream ) ; if ( connection != null ) { connection . disconnect ( ) ; } } }
downloads content from the specified url using specified proxy ( or do not using it ) and timeouts . returns null if there ' s an error .
