Flutter:使用凭据发布请求

时间:2018-03-21 04:48:05

标签: dart flutter

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

尝试使用以下代码

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

获取"防火墙读取失败或超时"错误

1 个答案:

答案 0 :(得分:2)

我用

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(...)