Get response body from a URL as a String
suggest changeString 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(
inputStream));
StringBuilder response = new StringBuilder();
String currentLine;
while ((currentLine = in.readLine()) != null)
response.append(currentLine);
in.close();
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, withnew URL(url).openConnection()
. We cast theUrlConnection
this returns to aHttpUrlConnection
, 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 fromInputStream
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.
Notes:
- 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 uncheckedMalformedUrlException
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.
Usage:
Is very simple:
String text = getText(”http://example.com");
//Do something with the text from example.com, in this case the HTML.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents