简体   繁体   中英

Error downloading images from Facebook Graph API with HTTPS with Android

I am not be able to download images with the HTTPS protocol due to a certificate error. This happens a couple of weeks or more, before that, I didn't have any problem.

If I use the HttpCommons Apache library, the error is "the hostname certificate didn't match" and if I use the URL and URLConnection the exception is "the certificate wasn't verified".

I'm desperated because I need to accomplish this to finish my project and all I have tested didn't work.

This is the code with the Apache library:

HttpClient client = new DefaultHttpClient();

HttpGet get = new HttpGet(url);

try
{
    HttpResponse response = client.execute(get);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
    {
        HttpEntity entity = response.getEntity();
        if (entity != null)
        {
            try
            {
                byte[] data = EntityUtils.toByteArray(entity);
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                //bitmap = BitmapFactory.decodeStream(entity.getContent());
            } catch (IOException e)
            {
                e.printStackTrace();
                Log.e("FACEBOOKLIBRARY", "Error fetching image from this URL: " + url
                        + "because of the download is corrupted");
                bitmap = null;
            } finally
            {
                entity.consumeContent();
            }
        }
    }
    else
    {
        Log.e("FACEBOOKLIBRARY", "Error fetching image from this URL: " + url
                + " because of the error: " + response.getStatusLine().toString());
        bitmap = null;
    }
} catch (IOException e)
{
    e.printStackTrace();
    Log.e("FACEBOOKLIBRARY", "Error fetching image while connecting to this URL: " + url);
    bitmap = null;
}

return bitmap;

And this is the URL code:

try {
    URL u = new URL(url);
    URLConnection connection = u.openConnection();
    InputStream is = connection.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);

    ByteArrayBuffer baf = new ByteArrayBuffer(50);
    int current = 0;
    while((current = bis.read()) != -1) {
        baf.append(current);
    }
    byte[] byteArray = baf.toByteArray();
    bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    bitmap = null;
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    bitmap = null;
}

I finally had to deactivate the hostname check to the HttpClient doing this:

HttpClient client = new DefaultHttpClient();

SSLSocketFactory sf = (SSLSocketFactory) client.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
sf.setHostnameVerifier(new AllowAllHostnameVerifier());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM