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.

6
  • Are you use HttpRequest in UI thread? Apr 29, 2012 at 17:39
  • No, I call the method in new AsyncTask<Void, Void, Boolean>() { @Override protected Boolean doInBackground(final Void... params) { //... }
    – Cornelius
    Apr 29, 2012 at 17:55
  • Your answer is found in this question: stackoverflow.com/questions/9365829/…
    – Kekoa
    May 9, 2012 at 14:37
  • @Kekoa OP is attempting a POST not a GET this question is unrelated
    – WiseGuy
    Jan 23, 2017 at 20:07
  • Have you found a solution for this problem? I'm experiencing the same at the moment. May 4, 2017 at 15:41

2 Answers 2

0
connection.setRequestMethod("POST");
connection.setDoOutput(false);

In above two statements just place

connection.setDoOutput(true)

before

connection.setRequestMethod("POST")

statement

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.