与AWS Cloudfront滑行,抛出403错误,但是当我使用okhttp发出请求时,得到了正确的响应

问题描述 投票:0回答:1

Glide 4.10.0

[当我尝试使用自定义Cookie标头以滑行方式将图像加载到imageview时,它会在尝试读取输入流时在httpurlfetcher.java类上引发403错误,但是当我使用okhttpclient发送相同的请求时,会获得响应码为200的正确响应,并且即使在浏览器中,我也可以查看图像。

在日志中,我找不到文件异常java.io.FileNotFoundException:https://d2q89b5pewg0ry.cloudfront.net/images/hikup.jpg但是,当我调试时,我在httpurlfetcher.java类中得到403

1。)滑行->图像未加载到图像视图中

List<String> cookies = Session.getInstance().getCookies(); GlideUrl glideUrl = new GlideUrl("https://d2q89b5pewg0ry.cloudfront.net/images/hikup.jpg", new LazyHeaders.Builder() .addHeader("Cookie", cookies.get(0)) .addHeader("Cookie", cookies.get(1)) .addHeader("Cookie", cookies.get(2)) .build()); Glide.with(this).load(glideUrl).error(android.R.color.white).into(profilePic);

2。)OkHttpClient->在这里我得到响应

OkHttpClient client = new OkHttpClient(); final Request request = new Request.Builder() .addHeader("Cookie", cookies.get(0)) .addHeader("Cookie", cookies.get(1)) .addHeader("Cookie", cookies.get(2)) .url("https://d2q89b5pewg0ry.cloudfront.net/images/hikup.jpg") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { runOnUiThread(() -> { Toast.makeText(ProfileActivity.this,e.toString(),Toast.LENGTH_LONG).show(); }); } @Override public void onResponse(Call call, final okhttp3.Response response) { runOnUiThread(() -> { Toast.makeText(ProfileActivity.this, response.toString(), Toast.LENGTH_LONG).show(); }); } });

当我调试httpurlfetcher.java类中的loadDataWithRedirects方法时>

urlConnection = connectionFactory.build(url);

//在这里,我放置一个断点并评估urlConnection.getResponseCode(),得到403即使在连接之前,如何获得响应代码?标头添加到下一行吗?

for (Map.Entry<String, String> headerEntry : headers.entrySet()) { urlConnection.addRequestProperty(headerEntry.getKey(), headerEntry.getValue()); } urlConnection.setConnectTimeout(timeout); urlConnection.setReadTimeout(timeout); urlConnection.setUseCaches(false); urlConnection.setDoInput(true); // Stop the urlConnection instance of HttpUrlConnection from following redirects so that // redirects will be handled by recursive calls to this method, loadDataWithRedirects. urlConnection.setInstanceFollowRedirects(false); // Connect explicitly to avoid errors in decoders if connection fails. urlConnection.connect(); // Set the stream so that it's closed in cleanup to avoid resource leaks. See #2352. stream = urlConnection.getInputStream();

Glide 4.10.0,当我尝试使用带有自定义Cookie标头的glide将图像加载到imageview时,它在尝试读取inputstream时在httpurlfetcher.java类上抛出403错误,但是当我发送相同的代码时...

android amazon-cloudfront android-glide
1个回答
0
投票

问题已解决,我使用了带有拦截器的okhttp集成库来设置标头。问题在于惰性标头,当存在相同的键时(在我的案例中为“ Cookie”),具有该键的所有标头都将合并,而AWS Cloudfront需要3个单独的Cookie标头。

© www.soinside.com 2019 - 2024. All rights reserved.