如何在Dart/Flutter中获取重定向URL? [英] How to get redirect URL in dart/flutter?
问题描述
我正在构建一个应用程序,该应用程序显示从source.unsplash拍摄的图像.我的请求网址是:
I'm building an app that shows an image taken from source.unsplash. My request url is:
https://source.unsplash.com/random/`$widthx$height`
然后,该服务重定向到一些适合我的宽度和高度的特定图像网址.
Then the service redirects to some specific image url picked fitting my width and height.
我可以通过以下方式显示图像: Image.network(上面的网址)
,
但是我无法访问代码中其他某些确实必要的新重定向URL.有办法吗?
I can show the image with:
Image.network(url above)
,
but I cannot access the new redirected url for some other parts of my code which is really necessary. Is there a way to get it?
预先感谢
我正在寻找的东西就像
Http.Response response = await Http.get(url);
print(response.finalRedirectURL);
推荐答案
如果您使用api.unsplash.com而不是源代码,那就更好了.
It would be better if you use api.unsplash.com and not source.
如果 followRedirects
为true,则根据文档无法获取最终重定向的URL,因此可能为此发布github问题.请参阅: https://pub.dev/documentation/http/latest/http/Response-class.html
If followRedirects
is true, there's no way according to the docs to get the final redirected URL, so maybe post a github issue for this. See: https://pub.dev/documentation/http/latest/http/Response-class.html
我看到这种情况的唯一方法是使 followRedirects:false
成为响应头,并以 location
作为键,为您提供下一个重定向.您必须编写一个循环,直到状态码从302更改为其他内容为止.
The only way I see this happening is making followRedirects: false
, will give you a response headers with location
as a key that gives you the next redirect. You'd have to write a loop to keep going till the status code changes from 302 to something else.
希望这会有所帮助.
这篇关于如何在Dart/Flutter中获取重定向URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!