首页 > 解决方案 > 如何发出正确的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)

标签: flutterhttpdart

解决方案


使用了错误的协议。

您已请求 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,
    },
  );

推荐阅读