在Android中使用httpUrlConnection发送授权标头 [英] Sending authorization headers using httpUrlConnection in Android

查看:144
本文介绍了在Android中使用httpUrlConnection发送授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在使用HttpUrlConnection时设置Authorization标头,但我在服务器上看到它为HTTP_AUTHORIZATION。

I am trying to set "Authorization" header while using HttpUrlConnection but I see it on the server as "HTTP_AUTHORIZATION".

这是我的代码:

public static String doGet(String params, String accessToken)
            throws MHNetworkException {
        try {
            URL url = new URL(getServerUrl());

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Authorization", "Token token="
                    + accessToken);
            conn.setRequestMethod("GET");

            int httpStatus = conn.getResponseCode();
            Log.v(tag, "httpStatus " + httpStatus);

            if (httpStatus == 200) {
                return MHIOUtil.getInputStreamAsString(conn.getInputStream());
            } 
        } catch (MalformedURLException me) {

        } catch (IOException ioe) {

        }
    }

BTWContent-Type标题也被更改为CONTENT_TYPE。

BTW the "Content-Type" header is also being changed to CONTENT_TYPE.

为什么Android发送更改它,是否有任何方式让我以我想要的方式发送它。或者我在这里做错了什么?如果重要的话,我们在后端使用nginx和Rails。

Why is Android sending changing it and is there any way for me to sent it exactly in the way I want. Or am I doing something wrong here? We are using nginx and Rails on the backend if that matters.

请不要建议使用HttpClient。我只是想使用HttpURLConnection,如果我可以让它工作,因为谷歌的Android开发人员说他们将把所有的努力都放在HttpURLConnection。

Please do not recommend using HttpClient. I just want to use HttpURLConnection if I can get this to work as Google's Android Developers said that they are going to put all their efforts in HttpURLConnection.

谢谢。

推荐答案

没有错。大多数标题都可以在前缀为HTTP_的服务器上访问,但Content-Type是专门处理的。 Android没有改变它,映射正在服务器上发生。映射到大写是标准的(标题的情况并不重要),将 - 替换为_。

There is nothing wrong. Most of the headers will be accessible on the server prefixed with HTTP_ but Content-Type is treated specially. Android is not changing it, the mapping is happening on the server. The mapping to upper case is standard (case of headers is not significant) as is the replacement of"-" by "_".

请参阅 CGI规范

这篇关于在Android中使用httpUrlConnection发送授权标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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