Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of setDoInput and setDoOutput in Java HttpURLConnection?

Tags:

java

What is the function or purpose of HttpURLConnection.setDoInput and HttpURLConnection.setDoOutput?

like image 447
Johnny Jazz Avatar asked Apr 07 '11 14:04

Johnny Jazz


People also ask

What is setDoInput in Java?

setDoInput(false) is called when the response body is going to be ignored. The Java core code does call this for JAXB and XML serialization to URLs.

What is setDoOutput?

setDoOutput(true) is used for POST and PUT requests. If it is false then it is for using GET requests. Follow this answer to receive notifications.

What is meant by URL connection?

URLConnection is an abstract class whose subclasses form the link between the user application and any resource on the web. We can use it to read/write from/to any resource referenced by a URL object. There are mainly two subclasses that extend the URLConnection class.


1 Answers

From the documentation for URLConnection:

Set the DoInput flag to true if you intend to use the URL connection for input, false if not. The default is true.

Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. The default is false.

As for the actual purpose, it doesn't appear that anything in the Oracle code checks on the doInput variable. However the doOutput default of false will prevent you from calling getOutputStream() necessary for HTTP POST requests. So it is a way to indicate ahead of time that you expect to write to an OutputStream without actually establishing it.

like image 200
WhiteFang34 Avatar answered Sep 22 '22 00:09

WhiteFang34