在Flutter应用中获取http获取请求的URL [英] Get URL of a http get request in flutter app
问题描述
我正在尝试获取以下get请求的URL;
I am trying to get the URL of the following get request;
String url = ['url_here'];
Future<Post> fetchPost() async {
final response = await http.get(url);
if (response.statusCode == 200) {
return Post.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load post');
}
}
我得到的状态码为200.
I get a statuscode of 200.
有什么想法吗?
为澄清起见,当您在浏览器中输入网址时,它将带我到一个带有其中包含代码的网址的页面.我正在尝试提取该代码.
To clarify, When you enter the url in a browser it brings me to a page with a url that has a code in it. I am trying to extract that code.
推荐答案
followRedirects
默认情况下为 true
,我还没有找到一种方法来获取其重定向的URL这样.
followRedirects
is true
by default and I haven't found a way to get the URL it redirects to this way.
设置 followRedirects = false
会在 location
标头中返回新地址.
Setting followRedirects = false
returns the new address in the location
header.
final url = 'http://wikipedia.net';
final client = http.Client();
final request = new http.Request('GET', Uri.parse(url))
..followRedirects = false;
final response = await client.send(request);
print(response.headers['location']);
print(response.statusCode);
如果要获取重定向地址的内容,则可以使用来自 location
的URL或仅使用默认值( followRedirects = true
)发送新请求.
If you want to fetch the content of the redirect address, you can send a new request with the URL from location
or just with the default (followRedirects = true
)
这篇关于在Flutter应用中获取http获取请求的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!