Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

android - 与AWS Cloudfront一起滑行,抛出403错误,但是当我使用okhttp发出请求时,我得到了正确的响应(Glide with aws cloudfront, throwing 403 error, but when i make request with okhttp i get proper response)

Glide 4.10.0

(滑翔4.10.0)

When i try to load image into imageview with glide with custom cookie headers it throws 403 error at httpurlfetcher.java class while trying to read inputstream, but when i send the same request with okhttpclient i get proper response with response code 200, and even in browser i am able to view image.

(当我尝试使用自定义Cookie标头以滑动方式将图像加载到imageview时,尝试读取inputstream时,它会在httpurlfetcher.java类上引发403错误,但是当我使用okhttpclient发送相同的请求时,我会获得正确的响应,响应码为200,甚至浏览器,我能够查看图像。)

In logs, i get file not found exception java.io.FileNotFoundException: https://d2q89b5pewg0ry.cloudfront.net/images/hikup.jpg But when i debug i get 403 in httpurlfetcher.java class

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

1.) Glide -> image is not loaded into imageview

(1.)Glide->图片未加载到imageview中)

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 -> here i get response

(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(); }); } });

When i debug loadDataWithRedirects method in httpurlfetcher.java class

(当我在httpurlfetcher.java类中调试loadDataWithRedirects方法时)

urlConnection = connectionFactory.build(url);

// here i put a breakpoint and evaluate urlConnection.getResponseCode(), i get 403 how am i getting responsecode even before connection?

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

headers are added in next line?

(标头添加到下一行?)

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();
  ask by Bharat Sharma translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...