public static String downloadString(URL url,Proxy proxy,int readTimeout,int socketTimeout,String encoding,int triesCount){
  IOException lastException=null;
  for (int i=0; i < triesCount; i++) {
    lastException=null;
    HttpURLConnection connection=null;
    InputStream inputStream=null;
    try {
      connection=(HttpURLConnection)(proxy == null ? url.openConnection() : url.openConnection(proxy));
      connection.setReadTimeout(readTimeout);
      connection.setConnectTimeout(socketTimeout);
      connection.connect();
      inputStream=connection.getInputStream();
      return IOUtils.toString(inputStream,encoding);
    }
 catch (    IOException ex) {
      if (LOG.isDebugEnabled()) {
        LOG.warn("Error downloading string from {}. Try number: {}. Cause:\r\n",url,triesCount,ex);
      }
 else {
        LOG.warn("Error downloading string from {}. Try number: {}. Cause:{}",url,triesCount,ex.getMessage());
      }
      lastException=ex;
    }
 finally {
      IOUtils.closeQuietly(inputStream);
      if (connection != null) {
        connection.disconnect();
      }
    }
  }
  if (lastException != null) {
    LOG.error("Could not download string from url: " + url,lastException);
  }
  return null;
}

public static String downloadString ( URL url , Proxy proxy , int readTimeout , int socketTimeout , String encoding , int triesCount ) { IOException lastException = null ; for ( int i = 0 ; i < triesCount ; i ++ ) { lastException = null ; HttpURLConnection connection = null ; InputStream inputStream = null ; try { connection = ( HttpURLConnection ) ( proxy == null ? url . openConnection ( ) : url . openConnection ( proxy ) ) ; connection . setReadTimeout ( readTimeout ) ; connection . setConnectTimeout ( socketTimeout ) ; connection . connect ( ) ; inputStream = connection . getInputStream ( ) ; return IOUtils . toString ( inputStream , encoding ) ; } catch ( IOException ex ) { if ( LOG . isDebugEnabled ( ) ) { LOG . warn ( "Error downloading string from {}. Try number: {}. Cause:\r\n" , url , triesCount , ex ) ; } else { LOG . warn ( "Error downloading string from {}. Try number: {}. Cause:{}" , url , triesCount , ex . getMessage ( ) ) ; } lastException = ex ; } finally { IOUtils . closeQuietly ( inputStream ) ; if ( connection != null ) { connection . disconnect ( ) ; } } } if ( lastException != null ) { LOG . error ( "Could not download string from url: " + url , lastException ) ; } return null ; }
downloads content from the specified url using specified proxy ( or do not using it ) and timeouts . returns null if there ' s an error .
