What exactly affects URLConnection.setDoOutput ()?

There setDoOutput() in a URLConnection . According to the documentation I have to

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

Now I am faced with exactly this problem - the Java runtime converts the request to POST after calling setDoOutput(true) and the server responds only to GET requests. I want to understand what happens if I remove this setDoOutput(true) from the code.

What exactly will this affect? Suppose I set it to false - what can I do now and what can not I do now? Will I be able to fulfill GET requests? What is "inference" in the context of this method?

+56
java android urlconnection
Dec 21 '11 at 9:53
source share
4 answers

You need to set it to true if you want to send (output) the body of the request, for example, with POST or PUT requests. With GET, you usually don't send the body, so you don't need it.

The request body is sent via the connection output stream:

 conn.getOutputStream().write(someBytes); 
+73
Dec 21 '11 at 9:59 a.m.
source share

setDoOutput(true) used for POST and PUT requests. If it is false , then it is used for GET requests.

+22
Dec 21 2018-11-12T00:
source share

Adding a comment if you have a long connection and you send GET and POST, this is what I do:

 if (doGet) { // some boolean con.setDoOutput(false); // reset any previous setting, if con is long lasting con.setRequestMethod("GET"); } else { con.setDoOutput(true); // reset any previous setting, if con is long lasting con.setRequestMethod("POST"); } 

To avoid a long connection, close it each time.

 if (doClose) // some boolean con.setRequestProperty("Connection", "close"); con.connect(); // force connect request 
+1
Feb 10 '16 at 15:44
source share
 public void setDoOutput( boolean dooutput ) 

It takes a value as a parameter and sets that value in the doOutput field for this URL connection with the specified value.

A URL connection can be used for input and / or output. Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. The default value is false.

0
Dec 21 2018-11-12T00:
source share



All Articles