【问题标题】:How to call URL in flutter https://key:secret@payment.api/payments/transactionid/update如何在颤振中调用 URL https://key:secret@payment.api/payments/transactionid/update
【发布时间】:2019-03-01 23:28:26
【问题描述】:

如何调用这样的 url https://key:secret@payment.api/payments/transactionid/update,我用 http.get(Uri.parse"https://key:secret@payment.api/payments/transactionid/update"); 但出现以下错误。

E/flutter (8892): [ERROR:flutter/shell/common/shell.cc(181)] Dart 错误:未处理的异常: E/flutter (8892): FormatException: Invalid port

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    URL 的key:secret 部分是身份验证凭据,在 Dart 中必须以不同方式处理。 Dart http 客户端支持身份验证,但由于这会导致服务器进行第二次往返,您可以自己添加身份验证标头。假设您的服务器正在等待Basic 身份验证,请尝试...

    void update() async {
      String username = 'key';
      String password = 'secret';
    
      http.Response r = await http.get(
        'https://payment.api/payments/transactionid/update',
        headers: {'authorization': basicAuthorizationHeader(username, password)},
      );
      print(r.statusCode);
      print(r.body);
    }
    
    String basicAuthorizationHeader(String username, String password) {
      return 'Basic ' + base64Encode(utf8.encode('$username:$password'));
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用以下代码调用 API 并告诉我:

      const _apiUrl = "http://example.com/api";
      Future getProductDetail(product_id) async {
      
        String apiUrl = '$_apiUrl/app/get-product?id=$product_id';
        http.Response response = await http.get(apiUrl,headers: {
          "Accept": "application/json",
        });
        print("Result: ${response.body}");
        return json.decode(response.body);
      }
      

      【讨论】:

      猜你喜欢
      • 2022-11-03
      • 2022-08-11
      • 2022-01-14
      • 2013-12-20
      • 2020-09-21
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      相关资源
      最近更新 更多