HttpURLConnection performs always a GET request instead of a POST request in spite of setDoOutput(true) and setRequestMethod("POST")
Asked Answered
F

2

9

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.

Flyweight answered 29/4, 2012 at 17:31 Comment(6)
Are you use HttpRequest in UI thread?Perugia
No, I call the method in new AsyncTask<Void, Void, Boolean>() { @Override protected Boolean doInBackground(final Void... params) { //... }Flyweight
Your answer is found in this question: #9366329Pellerin
@Pellerin OP is attempting a POST not a GET this question is unrelatedOxytetracycline
Have you found a solution for this problem? I'm experiencing the same at the moment.Contracture
@Contracture Unfortunately not.Flyweight
V
0
connection.setRequestMethod("POST");
connection.setDoOutput(false);

In above two statements just place

connection.setDoOutput(true)

before

connection.setRequestMethod("POST")

statement

Vendible answered 16/9, 2013 at 9:20 Comment(0)
F
0

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();
Foreboding answered 3/6, 2016 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.