如何在dart中使用HttpClient发出HTTPS请求?

时间:2014-02-11 13:36:19

标签: dart dart-html

我正在使用来自dart的HttpClient(dart:io package,not dart:http),我想发送一个HTTPS请求。有没有办法做到这一点?我似乎无法找到一种能让我这样做的方法。

4 个答案:

答案 0 :(得分:1)

我想向您推荐dio包,dio是Dart / Flutter的强大Http客户端,它支持拦截器,FormData,请求取消,文件下载,超时等。

dio非常易于使用:

执行POST请求:

response=await dio.post("/test",data:{"id":12,"name":"wendu"})

发送FormData:

FormData formData = new FormData.from({
   "name": "wendux",
   "age": 25,
});
response = await dio.post("/info", data: formData)

更多详情请参阅Github中的diohttps://github.com/flutterchina/dio

答案 1 :(得分:0)

new HttpClient().getUrl(Uri.parse('https://www.somedomain.com'));

答案 2 :(得分:0)

发送HTTPS请求的步骤与dart / flutter中的HTTP相同,您必须添加的一件事是允许自签名证书处理badCertificateCallback,并将其添加到HttpClient中:

var httpClient = HttpClient();
      httpClient.badCertificateCallback =
          ((X509Certificate cert, String host, int port) =>
              true); // Allow self signed certificates

参考:https://medium.com/@reme.lehane/flutter-using-self-signed-ssl-certificates-in-development-c3fe2d104acf

答案 3 :(得分:0)

HttpClient client = new HttpClient();
client.getUrl(Uri.parse("http://www.example.com/"))
    .then((HttpClientRequest request) {
      // Optionally set up headers...
      // Optionally write to the request object...
      // Then call close.
      ...
      return request.close();
    })
    .then((HttpClientResponse response) {
      // Process the response.
      ...
    });

返回:https://api.dart.dev/stable/2.13.1/dart-io/HttpClient-class.html