在请求中为通用图像加载器设置身份验证 Header

Set Authentication for universal image loader in Request Header

我正在使用 nostra13 延迟加载图像。我需要在请求 header 中指定用户名和密码才能访问受密码保护的图像。我在互联网上找到了一些步骤来完成此操作,但其中 none 对我有用。我覆盖了 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 "";
}

}

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

您可以 select 使用 DisplayImageOptions.extraForDownloader(headers).

设置身份验证的范围