一尘不染

Flutter:发布带有凭证的请求

flutter

我该如何从flutter中发出帖子请求。我需要使用其电子邮件地址和密码对用户进行身份验证。请帮忙

尝试使用以下代码

 http.post(url, body: {"email": "email", "password": "password"})
      .then((response) {
    print("Response status: ${response.statusCode}");
    //print("Response body: ${response.body}");
  });

收到“防火墙读取失败或超时”错误


阅读 231

收藏
2020-08-13

共1个答案

一尘不染

我用

import 'dart:async' show Future;
import 'dart:io'
    show HttpClient, HttpClientBasicCredentials, HttpClientCredentials;

import 'package:http/http.dart' show BaseClient, IOClient;

typedef Future<bool> HttpAuthenticationCallback(
    Uri uri, String scheme, String realm);

HttpAuthenticationCallback _basicAuthenticationCallback(
        HttpClient client, HttpClientCredentials credentials) =>
    (Uri uri, String scheme, String realm) {
      client.addCredentials(uri, realm, credentials);
      return new Future.value(true);
    };

BaseClient createBasicAuthenticationIoHttpClient(
    String userName, String password) {
  final credentials = new HttpClientBasicCredentials(userName, password);

  final client = new HttpClient();
  client.authenticate = _basicAuthenticationCallback(client, credentials);
  return new IOClient(client);
}



final http =
    createBasicAuthenticationIoHttpClient(_config.userName, _config.password);

http.get(...)
2020-08-13