HttpsURLConnection setDoOutput (true) not working

So, I am currently writing an Android application that will receive tweets from a specific user using Twitter 1.1 Application-only-auth , which requires a POST HTTP request to receive a carrier token. I have a method that uses HttpsURLConnectionto open a connection, but

  • conn.setDoOutput(true); does not work
  • conn.doOutput remains false
  • the request remains by default GET.

Am I doing something wrong?

This method is called in the method AsyncTask doInBackground():

public String getRequestBearerToken(String endUrl) throws IOException {
    String encodedCred = encodeKeys("API-key", "API-secret");
    HttpsURLConnection connection = null;
    try {
        URL url = new URL(endUrl);
        connection = (HttpsURLConnection) url.openConnection();

        connection.setInstanceFollowRedirects(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        connection.setRequestMethod("POST");


            // POST request properties
            connection.setRequestProperty("Host", "api.twitter.com");
            connection.setRequestProperty("User-Agent", getResources()
                    .getString(R.string.app_name));
            connection.setRequestProperty("Authorization", "Basic "
                    + encodedCred);
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded;charset=UTF-8");
            connection.setRequestProperty("Content-Length", "29");
            connection.setUseCaches(false);

            ...
+4
source share

Source: https://habr.com/ru/post/1533120/


All Articles