What Exactly Does Urlconnection.Setdooutput() Affect

What exactly does URLConnection.setDoOutput() affect?

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);

java.lang.IllegalStateException: Already connected at setDoOutput

According to this question setDoOutput is used for PUT and POST requests (they contain a request-entity-body).
So there is a conflict with your manually set request method GET.

You are using GET so the url-parameters should be set like in this question and you may just remove setDoOutput.

Also Content-Type does not make sense with GET-requests (source)

Java: how to use UrlConnection to post request with authorization?

A fine example found here. Powerlord got it right, below, for POST you need HttpURLConnection, instead.

Below is the code to do that,

    URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty ("Authorization", encodedCredentials);

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

writer.write(data);
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
writer.close();
reader.close();

Change URLConnection to HttpURLConnection, to make it POST request.

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");

Suggestion (...in comments):

You might need to set these properties too,

conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "Accept", "*/*" );

ResponseCode 400 for downloading image from url

I got my mistake.
Thank you everyone for sharing your ideas

con.setDoOutput(true); is a POST method
And it doesn't fetches any data

con.setDoOutput(true); should not be used.

Android HttpUrlConnection setting POST method

Solution

If you open a HttpUrlconnection on the "HTTPS" protocol, the connection permanently overrides the connection to "GET".

Via "HTTP" the connection will allow "POST" method if it is manually set.

HttpURLConnection GET request on Android gives weird 501 code

setDoOutput(true) is used for POST and PUT requests for sending (output) a request body. Usually we don't need this for GET requests. Found it here

Effect of HttpUrlConnection.setChunkedStreamingMode

  1. The HttpUrlConnection will send 4096 bytes to the server every write? and 1k for the last time?

Yes.


  1. Note that I have used a 10k buffer to write to the outputstream, Does it matter that the chunk size and buffer size are not the same?

No.


  1. If I disable the ChunkedStreamMode in my code, what's the effect compared to the code that I have set 4096?

The effect is that the entire output is buffered until you close, so that the Content-length header can be set and sent first, which adds a lot of latency and memory. Not recommended in the case of large files.



Related Topics



Leave a reply



Submit