Java URL Read readURLToString(String url)

Here you can find the source of readURLToString(String url)

Description

read URL To String

License

Open Source License

Declaration

public static String readURLToString(String url) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w w  w  .ja  v a2 s. c  o  m
 * Allmighty Bot - https://github.com/RyanTheAllmighty/AllmightyBot
 * Copyright (C) 2014 Ryan Dowling
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String readURLToString(String url) throws IOException {
        StringBuilder response = null;
        URL urll = new URL(url);
        URLConnection connection = urll.openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64)");
        connection.setConnectTimeout(5000);
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        response = new StringBuilder();
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        in.close();

        return response.toString();
    }
}

Related

  1. readURL(URL url)
  2. readUrl(URL url)
  3. readUrl(URL url, String[]... headers)
  4. readURL(URLConnection connection, String charset)
  5. readUrlContent(URLConnection connection)
  6. readUrlToString(String urlString)
  7. readVersion(URLConnection connection)
  8. retrieve(URL url)
  9. retrieveResponseMessage(HttpURLConnection connection)