0

I have this code inside a runnable, on Froyo everything works perfectly. But on ICS it says it does connect but gives me a file not found error and returns -1 as the file size.

There is no problem on ICS with a file that does not need authentication.

Is there a different way to authenticate on ICS ? or am I missing some ICS HttpURLConnection detail ?

Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass);
                    }
                });
URL url = new URL(URLString);

HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();//connection says that it is connected
final int filesize = c.getContentLength();//file size is -1 when using ICS
c.disconnect();

Also i'm having trouble authenticating to a https url on Froyo, thats a secondary concern at the moment though..

Thanks for helping if you can ..

I have been away for a while, but this is what I am using now. It does work with ICS, i'm not testing with Froyo at the moment so cant say for sure if it works with both ...

    private void setAuthenticationCredentials(final String username, final String password) {
            Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password
                            .toCharArray());
Kickaha
  • 3,680
  • 6
  • 38
  • 57

1 Answers1

0

I've been authenticating my requests usingHttpURLConnection.setRequestProperty() like this:

String unencoded = username + ":" + password;
String encoded = Base64.encodeToString(unencoded.getBytes(), Base64.DEFAULT);
Log.d(getClass().getName(), "encoded: " + encoded);
// Attach as authentication header.
connection.setRequestProperty("Authorization", "Basic " + encoded);

However, this seems to have the opposite problem. It works on 3.0 and higher but not on 2.3.4 and below (the server apparently sends a 400, although the request isn't in the server logs). Please tell me if you get this working/have already got it working, as I will be grateful for the information.

William Carter
  • 1,287
  • 12
  • 19
  • Thanks. I am using an `Authenticator` now as well and I am not seeing any problems. It's definitely the right way to do this. – William Carter Jun 05 '13 at 09:23