【问题标题】:Get URL of a http get request in flutter app在 Flutter 应用中获取 http get 请求的 URL
【发布时间】:2019-02-22 16:48:01
【问题描述】:

我正在尝试获取以下获取请求的 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 的页面,其中包含代码。我正在尝试提取该代码。

【问题讨论】:

  • 你能添加一些你的日志吗
  • 你想看什么?
  • 您使用的是来自dart:io 还是来自package:http 的HTTP?
  • import 'package:http/http.dart' as http;

标签: http url dart get flutter


【解决方案1】:

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 的 URL 或仅使用默认 (followRedirects = true) 发送一个新请求

【讨论】:

  • 重定向是假的
  • 响应码是什么? location 标头设置了吗?
  • 它是我可以用来测试自己的公共 URL 吗?
  • response.statusCode = 200,并且没有未设置位置标头
  • 它不公开。我正在尝试将 fitbit 连接到我的应用程序。在他们的 OAuth 2.0 教程页面上,他们使用 clientsecret 等构建 url。他们说:复制并粘贴用户单击“允许”按钮后您可以在重定向 URL 中找到的代码。示例:localhost:8888/…,您需要从该示例粘贴的代码是 7b64c4b088b9c841d15bcac15d4aa7433d35af3e。不要包含“#_=_”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 2021-09-12
  • 2019-11-21
  • 2021-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
相关资源
最近更新 更多