Android HttpUrlConnection does POST instead of GET

I have an Android app that is trying to execute a GET request to my server using HttpUrlConnection. When I test the code in a separate test desktop application, everything works fine. However, when I run it on my Android device, my server logs a POST request instead of GET.

Here is the code for my method get:

public static String get(String url) throws IOException {
    HttpURLConnection conn = connFromUrlString(url);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.connect();

    String out = IOUtils.toString(conn.getInputStream(), "UTF-8");
    conn.disconnect();
    return out;
}
+4
source share
1 answer

This line is the culprit.

conn.setDoOutput(true);

Remove it and try.

By the way, you should read this wonderful article: fooobar.com/questions/582 / ...

+10
source

All Articles