Java URL Read getHTML(String urlToRead)

Here you can find the source of getHTML(String urlToRead)

Description

Makes a get request to the provided url.

License

Open Source License

Parameter

Parameter Description
urlToRead The url to be accessed.

Return

The received content or an empty string.

Declaration

public static String getHTML(String urlToRead) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.*;

public class Main {
    /**/*  w  w w .java2s . co  m*/
     * Makes a get request to the provided url.
     * 
     * @param urlToRead
     *            The url to be accessed.
     * @return The received content or an empty string.
     */
    public static String getHTML(String urlToRead) {
        URL url;
        HttpURLConnection conn;
        BufferedReader rd;
        String line;
        StringBuilder result = new StringBuilder();
        try {
            url = new URL(urlToRead);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
        } catch (IOException e) {
            return "";
            // e.printStackTrace();
        } catch (Exception e) {
            return "";
            // e.printStackTrace();
        }
        return result.toString();
    }
}

Related

  1. getFilesizeFromUrl(String urlString)
  2. getFromURL(String URL)
  3. getHTML(String pageURL, String encoding)
  4. getHtml(String url)
  5. getHtml(String urlString)
  6. getHTML(String urlToRead)
  7. getHttpResponse(HttpURLConnection conn, int expectedReturnCode)
  8. getHTTPResponse(String url)
  9. getHttpResponse(String urlStr)