Java - URLConnection getDoOutput()
Description
The Java URLConnection getDoOutput() method returns the default value of a URLConnection's doOutput flag.
Declaration
Following is the declaration for java.net.URLConnection.getDoOutput() method
public boolean getDoOutput()
Parameters
NA
Return Value
the default value of a URLConnection's doOutput flag.
Exception
NA
Example 1
The following example shows the usage of Java URLConnection getDoOutput() 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 URLConnection instance. Using getDoOutput(), we're getting the default value of the doOutput flag of URLConnection instance and printing the same. Then we've changed the value of doOutput flag to true and printed the same again −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class UrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com");
URLConnection urlConnection = url.openConnection();
System.out.println(urlConnection.getDoOutput());
urlConnection.setDoOutput(true);
System.out.println(urlConnection.getDoOutput());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
false true
Example 2
The following example shows the usage of Java URLConnection getDoOutput() 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 URLConnection instance. Using getDoOutput(), we're getting the default value of the doOutput flag of URLConnection instance and printing the same. Then we've changed the value of doOutput flag to true and printed the same again −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class UrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("http://www.tutorialspoint.com");
URLConnection urlConnection = url.openConnection();
System.out.println(urlConnection.getDoOutput());
urlConnection.setDoOutput(true);
System.out.println(urlConnection.getDoOutput());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
false true
Example 3
The following example shows the usage of Java URLConnection getDoOutput() method for a url of google with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getDoOutput(), we're getting the default value of the doOutput flag of URLConnection instance and printing the same. Then we've changed the value of doOutput flag to true and printed the same again −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class UrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("http://www.google.com");
URLConnection urlConnection = url.openConnection();
System.out.println(urlConnection.getDoOutput());
urlConnection.setDoOutput(true);
System.out.println(urlConnection.getDoOutput());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
false true