http - 在 Flutter 应用中获取 http get 请求的 URL
问题描述
我正在尝试获取以下获取请求的 URL;
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 的状态码。
有任何想法吗?
澄清一下,当您在浏览器中输入 url 时,它会将我带到一个带有 url 的页面,其中包含代码。我正在尝试提取该代码。
解决方案
followRedirects
默认情况true
下,我还没有找到一种方法来获取它重定向到这种方式的 URL。
设置followRedirects = false
在标头中返回新地址location
。
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
或只使用默认 ( followRedirects = true
)的 URL
推荐阅读
- angular - 当组件以角度加载时如何选择输入字段的动态值?
- python - 按多列聚合值
- mysql - 如何在 symfony (mysql) 中优化诸如 LIKE 之类的搜索查询?
- node.js - Nodejs读取.xlsx文件给出了奇怪的符号
- javascript - 使用 *ngFor 的角度循环
- flutter - 颤振无限滚动总是重置视图
- php - 从网页(NGINX Web 服务器)调用 PHP 脚本时不会执行 bash 脚本
- swift - 未调用 DidSelectRow 函数
- fontforge - FontForge:如何添加隐藏的个人信息文本
- java - Gradle War 插件 - 重命名库