Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Read URL Contents using HttpURLConnection

Follow below steps to read the contents from an url.

Step 1: Create a connection to the url.
                 URL url = new URL(urlToConnect);
                 Httpurlconnection httpUrlConnection = (HttpURLConnection) url.openConnection();
                
Step 2: Create input stream from the url connection.

                 int responseCode = httpUrlConnection.getResponseCode();
                 Inputstream inputStream = null;

                 if (responseCode >= 200 && responseCode
                          inputStream = httpUrlConnection.getInputStream();
                 } else {
                          inputStream = httpUrlConnection.getErrorStream();
                 }

Step 3: Read the content from input stream and print them to console.

                 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

                 String line = null;

                 while ((line = br.readLine()) != null) {
                          System.out.println(line);
                 }


Find below working application.


Test.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

private static HttpURLConnection getURLConnection(String urlToConnect) throws IOException {
URL url = new URL(urlToConnect);
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
return httpUrlConnection;
}

private static InputStream getContent(HttpURLConnection httpUrlConnection) throws IOException {
int responseCode = httpUrlConnection.getResponseCode();
InputStream inputStream = null;

if (responseCode >= 200 && responseCode 400) {
inputStream = httpUrlConnection.getInputStream();
} else {
inputStream = httpUrlConnection.getErrorStream();
}

return inputStream;
}

private static void printInputStream(InputStream inputStream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

String line = null;

while ((line = br.readLine()) != null) {
System.out.println(line);
}
}

public static void main(String[] args) throws IOException {
String url = "https://self-learning-java-tutorial.blogspot.in/2016/05/java-home-page.html";
HttpURLConnection httpUrlConnection = getURLConnection(url);
InputStream inputStream = getContent(httpUrlConnection);
printInputStream(inputStream);

}

}



Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Read URL Contents using HttpURLConnection

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×