Java Language HttpURLConnection Get response body from a URL as a String

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


String getText(String url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    //add headers to the connection, or check the status if desired..
    // handle error response code it occurs
    int responseCode = conn.getResponseCode();
    InputStream inputStream;
    if (200 <= responseCode && responseCode <= 299) {
        inputStream = connection.getInputStream();
    } else {
        inputStream = connection.getErrorStream();

    BufferedReader in = new BufferedReader(
        new InputStreamReader(

    StringBuilder response = new StringBuilder();
    String currentLine;

    while ((currentLine = in.readLine()) != null) 


    return response.toString();

This will download text data from the specified URL, and return it as a String.

How this works:

  • First, we create a HttpUrlConnection from our URL, with new URL(url).openConnection(). We cast the UrlConnection this returns to a HttpUrlConnection, so we have access to things like adding headers (such as User Agent), or checking the response code. (This example does not do that, but it's easy to add.)

  • Then, create InputStream basing on the response code (for error handling)

  • Then, create a BufferedReader which allows us to read text from InputStream we get from the connection.

  • Now, we append the text to a StringBuilder, line by line.

  • Close the InputStream, and return the String we now have.


  • This method will throw an IoException in case of failure (such as a network error, or no internet connection), and it will also throw an unchecked MalformedUrlException if the given URL is not valid.

  • It can be used for reading from any URL which returns text, such as webpages (HTML), REST APIs which return JSON or XML, etc.

  • See also: Read URL to String in few lines of Java code.


Is very simple:

String text = getText(”");
//Do something with the text from, in this case the HTML.

Got any Java Language Question?