在请求标头中为通用图像加载器设置验证

时间:2015-02-09 11:39:17

标签: android universal-image-loader

我正在使用nostra13来延迟加载图片。我需要在请求标头中指定用户名和密码才能访问受密码保护的图像。我找到了一些在互联网上实现这一目标的步骤,但它们都没有为我工作。我重写了getStreamFromNetwork()方法并添加了用户名和密码,但它没有工作。

DisplayImageOptions options = new
DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisk(true).considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();

 ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(context)
.threadPoolSize(1)
.memoryCache(new WeakMemoryCache()).imageDownloader(new           
 CustomImageDownaloder(context,10000,10000))
.build();

   imageLoader=ImageLoader.getInstance();
   imageLoader.init(config);

以下是CustomImageDownloader的代码:

public class CustomImageDownaloder extends BaseImageDownloader {
public static final String TAG = CustomImageDownaloder.class.getName();


public CustomImageDownaloder(Context context
, int connectTimeout, int             
readTimeout) 
{
        super(context, connectTimeout, readTimeout);
}

@Override
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {

    URL url = null;
    try {
        url = new URL(imageUri);
    } catch (MalformedURLException e) {
        Log.e(TAG, e.getMessage(), e);
    }
    HttpURLConnection http = null;
    http.setRequestProperty("Authorization", "Basic " + encodeCredentials());

    if (Scheme.ofUri(imageUri) == Scheme.HTTPS) {
        trustAllHosts();
        HttpsURLConnection https = (HttpsURLConnection) url
                .openConnection();

        https.setHostnameVerifier(DO_NOT_VERIFY);
        http = https;
        http.connect();
    } else {
        http = (HttpURLConnection) url.openConnection();
    }
    http.setConnectTimeout(connectTimeout);
    http.setReadTimeout(readTimeout);
    return new FlushedInputStream(new BufferedInputStream(
            http.getInputStream()));
}

// always verify the host - dont check for certificate
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};

/**
 * Trust every server - dont check for any certificate
 */
private static void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(
                java.security.cert.X509Certificate[] x509Certificates,
                String s) throws java.security.cert.CertificateException {
        }

        @Override
        public void checkServerTrusted(
                java.security.cert.X509Certificate[] x509Certificates,
                String s) throws java.security.cert.CertificateException {
        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[] {};
        }
    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection
                .setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String encodeCredentials() {
    try {
        String userpass="";
        String auth = Base64.encodeToString(userpass.getBytes("UTF-8"), Base64.NO_WRAP);
        return auth;
    } catch (Exception ignored) {
        Log.e(TAG, ignored.getMessage(), ignored);
    }
    return "";
}

}

1 个答案:

答案 0 :(得分:1)

也许你可以试试这个: Accessing protected images in universal image loader

您可以使用DisplayImageOptions.extraForDownloader(标题)选择要设置auth的范围。