开发者

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

开发者 https://www.devze.com 2023-03-05 15:54 出处:网络
In my browser, or in iOS, when I try to get the contents of a URL with encoded http authentication information in the form

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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号