gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:21:46 26 4
gpt4 key购买 nike

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

您可以使用 DisplayImageOptions.extraForDownloader(headers) 选择要设置身份验证的范围。

关于android - 在请求 header 中为通用图像加载器设置身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com