5

因此,我目前正在编写一个 android 应用程序,该应用程序将使用 Twitter 的 1.1 Application-only-auth获取来自特定用户的推文,这需要 POST HTTP 请求来获取承载令牌。我有一个方法设置使用HttpsURLConnection打开连接,但是

  • conn.setDoOutput(true);方法不起作用
  • conn.doOutput停留false
  • 请求保持默认GET

难道我做错了什么?

AsyncTask此方法在 an的方法中调用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

0 回答 0