There's setDoOutput()
in URLConnection
. According to documentation I should
Set the DoOutput flag to true if you intend to use the URL connection for output, false if not.
Now I'm facing exactly this problem - the Java runtime converts the request to POST
once setDoOutput(true)
is called and the server only responds to GET
requests. I want to understand what happens if I remove that setDoOutput(true)
from the code.
What exactly will this affect? Suppose I set it to false
- what can I do now and what can't I do now? Will I be able to perform GET
requests? What is "output" in context of this method?
The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL. Instances of this class can be used both to read from and to write to the resource referenced by the URL.
getOutputStream()); If the URL does not support output, getOutputStream method throws an UnknownServiceException . If the URL does support output, then this method returns an output stream that is connected to the input stream of the URL on the server side — the client's output is the server's input.
URLConnection is an abstract class. The two subclasses HttpURLConnection and JarURLConnection makes the connetion between the client Java program and URL resource on the internet. With the help of URLConnection class, a user can read and write to and from any resource referenced by an URL object.
You need to set it to true if you want to send (output) a request body, for example with POST or PUT requests. With GET, you do not usually send a body, so you do not need it.
Sending the request body itself is done via the connection's output stream:
conn.getOutputStream().write(someBytes);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With