95

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?

4 Answers 4

115

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);
Sign up to request clarification or add additional context in comments.

2 Comments

What if I'm downloading an image ?
Downloading? Or uploading?
34

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

4 Comments

ok, how can I then force this connection to do PUT since server on the other side is accepting only PUT requests, not POST... ?
What if I'm downloading an image ?
Why is it named setDoOutput? Not a very intuitive name
@Jaydev if you are downloading, usually you only need to use GET. So doOutput can be set to false.
3

Adding a comment, if you have a long lasting connection and you send both GETs and POSTs, 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");
}

And to avoid making the connection long lasting, close it each time.

if (doClose)    // some boolean
    con.setRequestProperty("Connection", "close");

con.connect();              // force connect request

1 Comment

Ya hit on a pet peeve of mine there sonny. One call to setDoOutput: con.setDoOutput(!doGet);, rather than two with true/false value inside an if. The worst is if (foo) return true else return false, but this still treads on that particular toe.
0
public void setDoOutput( boolean dooutput )

It takes a value as the parameter and sets this value of the doOutput field for this URLConnection to 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 is false.

2 Comments

I've read that in the documentation before asking. The reason I'm asking is I don't get the implications of those.
If you want to write to a connection object you got to set the serDoOutput true

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.