How can I use http auth information from a URL in Android?

帅比萌擦擦* 提交于 2020-01-05 03:36:12

问题


In my browser, or in iOS, when I try to get the contents of a URL with encoded http authentication information in the form

http://myUser:myPassword@www.example.com/secure/area/index.html

It just works. I'm getting URLs from a web service, and I'd like to avoid trying to parse them up for their HTTP auth info if I can help it. Is there a way to do something similar in Android without actually parsing the URLs? Alternatively, what is the best way to go about that?

UPDATE: I find that when I try to set the authentication information in an Authorization header, I get a very strange FileNotFoundException.

Here's the code I'm using:

        URL url = new URL(urlString);
        URLConnection connection;
        String authority = url.getAuthority();
        if (authority.contains("@")) {
            String userPasswordString = authority.split("@")[0];
            url = new URL(urlString.replace(userPasswordString + "@", ""));
            connection = url.openConnection();
            String encoded = new String(Base64.encode(userPasswordString.getBytes(), Base64.DEFAULT), "UTF-8");
            connection.setRequestProperty("Authorization", "Basic " + encoded);
        } else {
            connection = url.openConnection();
        }

        InputStream responseStream = connection.getInputStream();

All the info seems to check out, I've verified the url is correct, the base64 string is correct, and the file is certainly on the server--I have no trouble at all opening it with Firefox, and Firebug shows all the right headers, matching what I've sent as far as I can tell. What I get though is the following error (url host changed to protect the innocent):

java.io.FileNotFoundException: http://a1b.example.com/grid/uploads/profile/avatar/user1/custom-avatar.jpg
  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)

Any idea what this is all about?

I looked into using HttpClient, but saw that in Issue 16041 it is recommended that we prefer URLConnection.


回答1:


That looks like your browser is applying some extra rules to parsing the URL. In Android you can use HTTP Client's authentication mechanism such as BASIC and DIGEST to do the same things. Which one you choose is dependent on the server you are trying to authenticate against.

Here is a good page to get you started.




回答2:


Unfortunately, on Android you can't pass the user info (username/password) in that format to either java.net.URL or HttpClient and have it work like in a browser.

I'd recommend using URI (see http://download.oracle.com/javase/1.5.0/docs/api/index.html?java/net/URI.html) to do this: pass your URL to the URI constructor that takes a String and then you can extract the user info (using getUserInfo()). You can then either use HttpClient's authorization classes (see http://developer.android.com/reference/org/apache/http/auth/package-summary.html) or build the basic auth header yourself (an example is given at http://www.avajava.com/tutorials/lessons/how-do-i-connect-to-a-url-using-basic-authentication.html).



来源:https://stackoverflow.com/questions/5979087/how-can-i-use-http-auth-information-from-a-url-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!