Home » Android » java – HttpURLConnection performs always a GET request instead of a POST request in spite of setDoOutput(true) and setRequestMethod("POST")

java – HttpURLConnection performs always a GET request instead of a POST request in spite of setDoOutput(true) and setRequestMethod("POST")

Posted by: admin June 15, 2020 Leave a comment

Questions:

Since update to Ice Cream Sandwich, my POST request doesn’t work anymore. Before ICS, this works fine:

try {
        final URL url = new URL("http://example.com/api/user");
        final HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(false);
        connection.setDoInput(true);
        connection.setRequestProperty("Content-Length", "0");
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Log.w(RestUploader.class.getSimpleName(), ": response code: " + connection.getResponseMessage());
        } else {
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
            final String line = reader.readLine();
            reader.close();
            return Long.parseLong(line);
        }
    } catch (final MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return -1;

I’ve tried to set

connection.setDoOutput(true);

but it doesn’t works. The server response is always a 405 (Method not allowed) and the server log says it was an GET request.

The Android JavaDoc to setRequestMethod says:

This method can only be called before the connection is made.

Does it mean that the method must be invoked before url.openConnection()? How should I create a HttpURLConnection instance? All the examples I’ve seen, make it as described above.

I hope someone has an idea why it always send a GET request instead of a POST.

Thanks in advance.

How to&Answers:
connection.setRequestMethod("POST");
connection.setDoOutput(false);

In above two statements just place

connection.setDoOutput(true)

before

connection.setRequestMethod("POST")

statement

Answer:

My .htaccess had a rewrite rule which redirected www to http://. Turned out my Java code worked just fine! The redirect/rewrite rule made my 1st request (POST) to forward to http and therefore “became” a GET. My PHP script was listening to POST only, and man did I scratch my head until I found my own mistake having that redirect rule. Check yours for this kind of “problem”.

For me, setting either setDoOutput or setRequestMethod first does not matter, any order works (API 23). At the moment things are in this order:

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setInstanceFollowRedirects(false);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type",bla bla
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setRequestProperty("Content-Length", String.valueOf(PARAMETER_VARIABLE.getBytes().length));
con.setFixedLengthStreamingMode(PARAMETER_VARIABLE.getBytes().length);
OutputStream os = con.getOutputStream();
os.write(PARAMETER_VARIABLE.getBytes());
os.flush();
os.close();