flutter - 如何发出正确的http请求?
问题描述
我似乎无法用我的代码发出正确的 http 请求。每当我运行以下代码时,
var url = Uri.https('datamall2.mytransport.sg', '/ltaodataservice/BusArrivalv2?BusStopCode=', {'BusStopCode': busStopCode});
final response = await http.get(
url,
headers: <String, String>{
'Content-Type': 'application/json',
'AccountKey': accountKey,
},
);
我收到以下错误
I/flutter (24816): SocketException: OS Error: Connection refused, errno = 111, address = datamall2.mytransport.sg
这是我试图发出的 http 请求
http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2?BusStopCode=busStopCode (busStopCode is a 5 digit number)
解决方案
使用了错误的协议。
您已请求 httpS。服务器只响应http。
您可以尝试一些更简单的方法:
String url = 'http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2?$busStopCode';
final response = await http.get(
url,
headers: <String, String>{
'Content-Type': 'application/json',
'AccountKey': accountKey,
},
);
推荐阅读
- c# - 如何为 Azure 应用程序设置权限,以便应用程序可以访问站点及其驱动器?
- flutter - 在颤动中将对象转换为数组
- amazon-web-services - AWS DynamoDB 流:我可以将记录保存超过 24 小时吗?
- php - PHP:curl 脚本失败
- jquery - Gatsby JS:预加载器永远加载
- javascript - .map() 方法在项目之间打印“,”
- silverstripe - 从夹具文件生成的字段列表中的未知列
- python - Windows 上的 Visual Studio Code 远程 Python 调试
- javascript - Jquery脚本不适用于表单提交
- c++ - 如何阻止 C++ 控制台输出“tick”