Java: FileNotFoundException when loading Bitmap from URL with basic authentication

I know that there are quite a few such questions, but none of them worked for me :(

In my Android app, I am trying to get Drawable / Bitmap from a url that happens as follows:

public Bitmap loadBitmap(ISettings settings) {
    Bitmap bm = null;
    InputStream is = null;
    BufferedInputStream bis = null;

    final URL imageURL = new URL("http://domain.com/profile/image");
    final String authString = settings.getUserName() + ":" + settings.getPassword();
    final String authStringEnc = Base64.encodeToString(authString.getBytes(), Base64.DEFAULT);

    URLConnection conn = url.openConnection();
    conn.setRequestProperty("Authorization", "Basic " + authStringEnc);
    conn.connect();
    is = conn.getInputStream();
    bis = new BufferedInputStream(is, 8192);
    bm = BitmapFactory.decodeStream(bis);
    return bm;
}

However, every time I run this child, something unexpected happens:

java.io.FileNotFoundException: http://domain.com/profile/image

How can it help you, here is the twisting of the resource

curl -u Username:Password http://domain.com/profile/image

and its output in wirehark:

GET /profile/image HTTP/1.1
Authorization: Basic BASICAUTHSTRING
User-Agent: curl/7.25.0 (i386-pc-win32) libcurl/7.25.0 OpenSSL/0.9.8u zlib/1.2.6 libidn/1.18 libssh2/1.4.0 librtmp/2.3
Host: domain.com
Accept: */*

HTTP/1.1 200 OK
Date: Mon, 21 May 2012 18:36:11 GMT
Server: Apache/2.2.14 (Ubuntu)
Set-Cookie: JSESSIONID=65178AC10C59F372FEC901EBB71F38F7; Path=/profile
Content-Disposition: attachment; filename="profile.name.png"
Content-Length: 33195
Content-Type: image/png;charset=UTF-8

‰PNG
.. just the bytes of png which I cannot enter here as it would be too long :)

How can this be solved?

EDIT # 1:

Capturing traffic from the phone to the network, I see this, which is a little obvious :) - but I do not know how to do it correctly:

GET /profile/image HTTP/1.1
Authorization: Basic BASICAUTHSTRING
User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.7; Nexus One Build/GRK39F)
Host: domain.com
Connection: Keep-Alive
Accept-Encoding: gzip

HTTP/1.1 400 Bad Request    
Date: Mon, 21 May 2012 23:01:42 GMT    
Server: Apache/2.2.14 (Ubuntu)    
Vary: Accept-Encoding
Content-Length: 327    
Connection: close    
Content-Type: text/html; charset=iso-8859-1   

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.2.14 (Ubuntu) Server at domain.com Port 80</address>
</body></html>

Edit # 2:

What is the witcher? Using curl and using EXACT SAME Header, as on the device:

curl -H "Connection: Keep-Alive" -H "Accept-Encoding: gzip" -H "Accept:" -H "User-Agent: curl/7.25.0 (i386-pc-win32) libcurl/7.25.0 OpenSSL/0.9.8u zlib/1.2.6 libidn/1.18 libssh2/1.4.0 librtmp/2.3" -u Username:Password http://domain.com/profile/image

I get the image right away: <

What's going on here?

+3
1

, .

, BASICAUTHSTRING - , .

android.util.Base64.encodeToString(authString.getBytes(), android.util.Base64.DEFAULT)

.

android.util.Base64.No_WRAP

.

, :)

+5

All Articles