Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does URLConnection.setDoOutput() affect?

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?

like image 636
sharptooth Avatar asked Dec 21 '11 09:12

sharptooth


People also ask

What is meant by URL connection?

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.

Which method returns the output stream of the URL connection?

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.

Which of the following abstract classes is the superclass of all classes that read an object from URL connection?

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.


1 Answers

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); 
like image 101
Thilo Avatar answered Sep 21 '22 21:09

Thilo