微信交流群

老孟公众号

本文介绍如何使用 HttpClient 进行网络请求,HttpClient 是 Dart标准库的一部分,无需引入第三方,但其本身功能较弱,对开发者不友好,使用比较麻烦,因此并不建议直接使用 HttpClient 进行网络请求。

使用 HttpClient 进行网络请求步骤如下:

  1. 创建HttpClient:

    import 'dart:io';
    
    var httpClient = new HttpClient();
    
    1
    2
    3

    HttpClient需要导入dart:io包。

  2. 构建Uri:

    var uri = Uri(scheme: 'https',host: 'www.xxx.com', queryParameters: {
      'params1:': '',
      'params2:': '',
    });
    
    1
    2
    3
    4
    • host:表示服务器的域名。
    • queryParameters:表示请求参数。
  3. 打开http连接:

    HttpClientRequest request = await httpClient.getUrl(uri);
    
    1
  4. 如果需要,设置 header,也可不设置:

    request.headers.add('name', 'value');
    
    1
  5. 关闭请求, 等待响应:

    HttpClientResponse response = await request.close();
    
    1
  6. 解析返回的数据:

    String responseBody = await response.transform(utf8.decoder).join();
    
    1

下面通过 HttpClient 获取 https://github.com/781238222/flutter-do/blob/master/README.md 内容:

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';

///
/// desc:
///

class HttpClientDemo extends StatefulWidget {
  
  _HttpClientDemoState createState() => _HttpClientDemoState();
}

class _HttpClientDemoState extends State<HttpClientDemo> {
  String _data;

  getData() async {
    var httpClient = new HttpClient();
    var uri = Uri(
        scheme: 'https',
        host: 'github.com',
        path: '781238222/flutter-do/blob/master/README.md');

    HttpClientRequest request = await httpClient.getUrl(uri);
    request.headers.add('name', 'value');
    HttpClientResponse response = await request.close();
    String responseBody = await response.transform(utf8.decoder).join();
    print('responseBody:$responseBody');
    setState(() {
      _data = responseBody;
    });
  }

  
  void initState() {
    super.initState();
    getData();
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('HttpClient'),
      ),
      body: Center(
        child: Text('$_data'),
      ),
    );
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52