【问题标题】:Making Https request from the flutter app?从颤振应用程序发出 Https 请求?
【发布时间】:2020-08-12 08:17:06
【问题描述】:

我的 API 已部署在服务器中,我可以发出 HTTP、https 请求来访问 API。 我已经通过发出 http 请求测试了 api。喜欢

String loginURL="https://<ip>/login/auth";
http.Response response = await http.post(loginURL,
          headers: {"Content-type": "application/json"},
          body: json.encode({"username": username, "password": password}));

如果我完成了 http 请求,我会收到成功代码为 200 的响应

但是当我尝试访问使用 https 部署的同一个 API 时。我在服务器中收到异常,没有收到任何响应。如何发起 https 请求?

【问题讨论】:

  • 服务器证书是否被信任库中的证书信任?还是自签?
  • 它的自签名
  • 好的,除非您在 Android 操作系统中信任它,否则您将正确地收到无效证书错误。
  • 我在android模拟器中执行了代码,但我无法登录,它显示来自catch块的无法连接错误消息,如何信任android设备
  • 在从邮递员那里访问 api 时我没有收到响应,然后我关闭了 SSL 证书验证然后我得到了响应

标签: api http flutter dart https


【解决方案1】:

使用这个 HTTP 模板

import 'dart:convert';
import 'package:http/http.dart' as http;

class RegisterApi {
 static Future<Map> login(String email, String password, String name) async {
   var url =
      'xxxxxxxxxx';

var header = {
  "Content-type": "application/json",
  "key": "xxxxxxxxxx",
  "token": "xxxxxxxxxxxx"
};

Map params = {
  "anunciante_name": name,
  "anunciante_pass": password,
  "anunciante_email": email
};

try {
  var _body = json.encode(params);

  var response = await http.post(url, headers: header, body: _body);

  var data = json.decode(response.body);

  return data;
   } catch (e) {
   return null;
  }
 }
}

This link will also help

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 2019-12-19
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2021-05-28
    • 2021-07-12
    相关资源
    最近更新 更多