【问题标题】:how to set authorization header with android's httpURLconnection如何使用 android 的 httpURLconnection 设置授权标头
【发布时间】:2015-12-21 18:59:49
【问题描述】:

我正在尝试使用基本身份验证连接到我的服务器,但是当我设置 Authorization 标头时,它会导致 getInputStream 引发 fileNotFound 异常。

以下是相关代码:

        URL url = new URL(myurl);
        //set up the connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);//this is in milliseconds
        conn.setConnectTimeout(15000);//this is in milliseconds
        String authHeader = getAuthHeader(userID,pass);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.setRequestProperty("authorization", authHeader);
        //starts the query
        conn.connect();
        int response = conn.getResponseCode();
        is = conn.getInputStream(); //throws the fileNotFound exception

这里是抛出的异常:

java.io.FileNotFoundException: http://<myIPaddress>/login/

奇怪的是,我发现 如果我尝试将请求属性设置为“授权”或“授权”或该词的任何变体,则会引发 fileNotFound 异常。如果我将它设置为“content-type”或“authosodifjsodfjs”(一个随机字符串),它不会抛出,如下所示:

conn.setRequestProperty("content-type",authHeader)//no exception thrown
conn.setRequestProperty("authosodifjsodfjs",authHeader)//no exception thrown

如果我不设置此标头,我可以使用此代码连接到服务器并获得我期望的正确的拒绝访问消息。 如果我使用python的“请求”模块,我也可以连接到服务器并正确登录,所以这不是服务器的问题。

所以我的问题如下:

1) 将请求属性设置为“授权”时,我做错了什么(如果有的话)?如何正确设置 auth 标头?

2) 如果这是 HttpURLConnection 的错误,我该如何提交错误报告?

谢谢。

编辑:建议从以下位置切换:

conn.setRequestProperty("Authorization", authHeader);

到:

conn.addRequestProperty("Authorization", authHeader);

这并没有解决问题。它仍然抛出同样的异常。

编辑: 仍然不确定为什么“授权”和“授权”会导致 fileNotFounExceptions,但使用全部大写似乎可以正常工作。这是闪亮的新工作代码:

conn.setRequestProperty("AUTHORIZATION",authHeader);

所以看起来它需要全部大写。 “HTTP_”会自动添加到这个header的前面,所以服务器看到的header就是“HTTP_AUTHORIZATION”,应该是这样的。

【问题讨论】:

  • getAuthHeader(String, String) 返回什么?
  • private String getAuthHeader(String ID, String pass){ String encoded = Base64.encodeToString((ID + ":" + pass).getBytes(), Base64.NO_WRAP); String returnThis = "Basic " + encoded; return returnThis; }

标签: android httpurlconnection filenotfoundexception basic-authentication


【解决方案1】:

这是我设置授权标头的 OAuth 代码的一部分:

httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestProperty("User-Agent", "MyAgent");
httpUrlConnection.setConnectTimeout(30000);
httpUrlConnection.setReadTimeout(30000);

String baseAuthStr = APIKEY + ":" + APISECRET;
httpUrlConnection.addRequestProperty("Authorization", "Basic " + baseAuthStr);
httpUrlConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

httpUrlConnection.connect();

【讨论】:

  • 感谢您的回复。您是否设置了第二种类型的连接?您正在向“urlConnection”添加请求属性,但正在与“httpUrlConnection”连接..?
  • @JoeRyan httpUrlConnection 和你的 conn 一样
  • 谢谢马尔辛。我从“setRequestProperty()”切换到“addRequestProperty()”,就像你的代码一样,但不幸的是它没有解决任何问题。
  • @JoeRyan 我想您应该在调用 getInputStream 之前检查响应 - 并且仅使用响应代码 == 200 调用它。如果它不同,则使用 getErrorStream。至少我在我的代码中是这样做的。
  • 所以我尝试将标头直接设置为“HTTP_AUTHORIZATION”,但这不起作用,但查看了服务器正在获取的标头,结果发现android只是在标头前加上“HTTP_” ...无论如何,我将标题更改为“AUTHORIZATION”(全部大写),它神奇地起作用了。我会把答案归功于你。谢谢
猜你喜欢
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多