getLine method

Future<void> getLine()

Implementation

Future<void> getLine() async {
  bool foundLine = false;
  int myStep2Index = 0;

  for (String txtUrl in urlList) {
    if (usedLine || foundLine) {
      break;
    }

    String url = checkUrl('$txtUrl/v1/api/verify');

    if (url.isEmpty) {
      print('无效的地址: $txtUrl');
      continue;
    }

    String uuid = Uuid().v4();

    Map<String, String> headers = {
      "Content-Type": "application/json",
      "x-trace-id": uuid
    };

    try {
      var response = await http.post(Uri.parse(url),
          headers: headers, body: jsonEncode(bodyStr));

      if (response.statusCode == 200 && response.body.contains("tenantId")) {
        foundLine = true;

        if (!usedLine) {
          usedLine = true;
          var uri = Uri.parse(url);
          String line = uri.host;

          if (uri.port != 80 && uri.port != 443) {
            line = "$line:${uri.port}";
          }

          delegate?.useTheLine(line);
          print("使用线路: $line");
        }
      } else {
        print("线路失败: $url, 响应数据错误");
        myStep2Index += 1;
        if (myStep2Index == urlList.length) {
          failedAndRetry();
        }
      }
    } catch (error) {
      print(error);
      myStep2Index += 1;
      if (myStep2Index == urlList.length) {
        failedAndRetry();
      }
    }
  }
}