getRequestProperty(“Authorization”) always returns null

前端 未结 3 514
感动是毒
感动是毒 2020-12-15 20:42

I am trying to read the authorization header for an HTTP request (because I need to add something to it), but I always get null for the header value. Other headers work fine

3条回答
  •  感情败类
    2020-12-15 21:21

    Apparently, it's a security "feature". The URLConnection is actually an instance of sun.net.www.protocol.http.HttpURLConnection. It defines getRequestProperty as:

        public String getRequestProperty (String key) {
            // don't return headers containing security sensitive information
            if (key != null) {
                for (int i=0; i < EXCLUDE_HEADERS.length; i++) {
                    if (key.equalsIgnoreCase(EXCLUDE_HEADERS[i])) {
                        return null;
                    }
                }
            }
            return requests.findValue(key);
        }
    

    The EXCLUDE_HEADERS array is defined as:

       // the following http request headers should NOT have their values
       // returned for security reasons.
       private static final String[] EXCLUDE_HEADERS = {
               "Proxy-Authorization",
               "Authorization"
       };
    

提交回复
热议问题