Sending authorization headers with httpUrlConnection in Android

I'm trying to set the "Authorization" header when using HttpUrlConnection, but I see it on the server as "HTTP_AUTHORIZATION".

Here is my code:

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) {

        }
    }

      

By the way, the Content-Type header also changes to CONTENT_TYPE.

Why does sending Android change it, and is there a way to send it exactly the way I want. Or am I doing something wrong? We use nginx and Rails on the backend if that matters.

Please, it is not recommended to use HttpClient. I just want to use HttpURLConnection if I can get this to work, as the Google Android developers have stated that they are going to put their best effort into HttpURLConnection.

Thank.

+3


source to share


1 answer


There is nothing bad. Most of the headers will be available on the server with the HTTP_ prefix, but the Content-Type is handled specifically. Android does not change it, the mapping happens on the server. Mapping to uppercase is standard (the case of headers does not matter), as is replacing "-" with "_".



See the CGI specification .

+2


source







All Articles