Android 4.0 ICS 将 HttpURLConnection GET 请求转换为 POST 请求 [英] Android 4.0 ICS turning HttpURLConnection GET requests into POST requests

查看:22
本文介绍了Android 4.0 ICS 将 HttpURLConnection GET 请求转换为 POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Galaxy Nexus 今天到货了,我做的第一件事就是将我的应用加载到它上面,这样我就可以向我的朋友们展示它.其部分功能涉及从 Google 阅读器导入 RSS 提要.但是,在尝试此操作时,我收到了 405 Method Not Allowed 错误.

My Galaxy Nexus arrived today, and one of the first things I did was to load my app onto it so I could demonstrate it to my friends. Part of its functionality involves importing RSS Feeds from Google Reader. However, upon trying this, I was getting 405 Method Not Allowed errors.

这个问题是冰淇淋三明治特有的.我附加的代码在 Gingerbread 和 Honeycomb 上运行良好.我已经将错误追溯到建立连接的那一刻,当时 GET 请求神奇地变成了 POST 请求.

This problem is Ice Cream Sandwich-specific. The code I've attached works fine on Gingerbread and Honeycomb. I've traced the error down to the moment the connection is made, when the GET request magically turns into a POST request.

/**
 * Get the authentication token from Google
 * @param auth The Auth Key generated in getAuth()
 * @return The authentication token
 */
private String getToken(String auth) {
    final String tokenAddress = "https://www.google.com/reader/api/0/token";
    String response = "";
    URL tokenUrl;

    try {
        tokenUrl = new URL(tokenAddress);
        HttpURLConnection connection = (HttpURLConnection) tokenUrl.openConnection();

        connection.setRequestMethod("GET");
        connection.addRequestProperty("Authorization", "GoogleLogin auth=" + auth);
        connection.setRequestProperty("Content-Type","application/x-www-form-urlendcoded");
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        Log.d(TAG, "Initial method: " + connection.getRequestMethod()); // Still GET at this point

        try {
            connection.connect();
            Log.d(TAG, "Connected. Method is: " + connection.getRequestMethod());  // Has now turned into POST, causing the 405 error
            InputStream in = new BufferedInputStream(connection.getInputStream());
            response = convertStreamToString(in);
            connection.disconnect();
            return response;

        }
        catch (Exception e) {
            Log.d(TAG, "Something bad happened, response code was " + connection.getResponseCode()); // Error 405
            Log.d(TAG, "Method was " + connection.getRequestMethod()); // POST again
            Log.d(TAG, "Auth string was " + auth);
            e.printStackTrace();
            connection.disconnect();
            return null;
        }
    }
    catch(Exception e) {
        // Stuff
        Log.d(TAG, "Something bad happened.");
        e.printStackTrace();
        return null;
    }
}

是否有任何可能导致此问题的原因?能否更好地编写此函数以避免此问题?

Is there anything that could be causing this problem? Could this function be better coded to avoid this problem?

非常感谢.

推荐答案

此行为在 Android 开发者:HttpURLConnection

HttpURLConnection 默认使用 GET 方法.它将使用 POST 如果setDoOutput(true) 已被调用.

HttpURLConnection uses the GET method by default. It will use POST if setDoOutput(true) has been called.

奇怪的是,这实际上直到 4.0 才成为这种行为,所以我想它会破坏许多现有的已发布应用程序.

What's strange though is that this has not actually been the behaviour until 4.0, so I would imagine it's going to break many existing published apps.

Android 4.0 将 GET 转换为 POST.

这篇关于Android 4.0 ICS 将 HttpURLConnection GET 请求转换为 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆