Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - HttpURLConnection setChunkedStreamingMode()



The Java HttpURLConnection setChunkedStreamingMode() method enables streaming of a HTTP request body without internal buffering, when the content length is not known in advance. In this mode, chunked transfer encoding is used to send the request body. Note, not all HTTP servers support this mode.

When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection are required. This exception can be queried for the details of the error. This method must be called before the URLConnection is connected.

Declaration

Following is the declaration for java.net.HttpURLConnection.setChunkedStreamingMode() method

public void setChunkedStreamingMode(int chunklen)

Parameters

chunklen − The number of bytes to write in each chunk. If chunklen is less than or equal to zero, a default value will be used.

Return Value

NA

Exception

IllegalStateException − if URLConnection is already connected or if a different streaming mode is already enabled.

Example 1

The following example shows the usage of Java HttpURLConnection setChunkedStreamingMode() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getInputStream(), we're getting the content of the website home page and printing the same −

package com.tutorialspoint;

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

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index1.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setChunkedStreamingMode(1000);  
		 urlConnection.setDoOutput(true);
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getInputStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getInputStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Input Stream is null");
         }
         urlConnection.disconnect();  
         System.out.println("Disconnected.");  
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Connected.
<!DOCTYPE html><html lang="en"><head><title>Online Tutorials Library....
Disconnected.

Example 2

The following example shows the usage of Java HttpURLConnection setChunkedStreamingMode() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getInputStream(), we're getting the content of the website home page and printing the same −

package com.tutorialspoint;

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

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com/index1.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setChunkedStreamingMode(1000);  
		 urlConnection.setDoOutput(true);
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getInputStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getInputStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Input Stream is null");
         }
         urlConnection.disconnect();  
         System.out.println("Disconnected.");  
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Connected.
<!DOCTYPE html><html lang="en"><head><title>Online Tutorials Library....
Disconnected.

Example 3

The following example shows the usage of Java HttpURLConnection setChunkedStreamingMode() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getInputStream(), we're getting the content of the website home page and printing the same −

package com.tutorialspoint;

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

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setChunkedStreamingMode(1000);  
		 urlConnection.setDoOutput(true);
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getInputStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getInputStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Input Stream is null");
         }
         urlConnection.disconnect();  
         System.out.println("Disconnected.");  
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Connected.
java.io.IOException: Server returned HTTP response code: 405 for URL: http://www.google.com
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:18)
java_httpurlconnection.htm
Advertisements