Thursday, June 30, 2022

Read URL Contents using HttpURLConnection

Working with HttpURLConnection class

HttpURLConnection class is used to establish a connection to http end points. By using HttpURLConnection class, you can open a connection to given http end point, read the content, write content, read and write headers, read response status codes etc., In this tutorial series, you will get hands on of all these things.

·       Read URL Contents using HttpURLConnection
·       Get headers from url using HttpURLConnection
·       Adding headers using HttpURLConnection
·       Post data to url 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 < 400) {
                          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.com/2016/05/java-home-page.html";
  HttpURLConnection httpUrlConnection = getURLConnection(url);
  InputStream inputStream = getContent(httpUrlConnection);
  printInputStream(inputStream);

 }

}

No comments:

Post a Comment

Java NIO - Overview

Java IO (Input/Output) is used to perform read and write operations. The  java.io package  contains all the classes required for input and o...