Download a Webpage in Java

public static String downloadFromUrl(String my_url) {

    URL url;
    InputStream is = null;
    BufferedReader br;
    String line;
    String content = "";

    try {
        url = new URL(my_url);
        is = url.openStream(); // throws an IOException
        br = new BufferedReader(new InputStreamReader(is));

        while ((line = br.readLine()) != null) {
            content += line;
        }
    } catch (MalformedURLException mue) {
        return null;
    } catch (IOException ioe) {
        return null;
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ioe) {
            // nothing to see here
        }
    }

    return content;
}

Leave a Comment

Your email address will not be published. Required fields are marked *