Dart基础语法-网络请求

Dart基础语法-网络请求

祝大家中秋愉快, 本来打算出去溜娃,但娃不配合,无奈拿起电脑,简单更一篇文章吧。
上周没有更文是因为公司项目时间很紧,下边废话不多说, 简单说一下get请求和post 请求 。

get 请求

  /**
   * 向目标主机发送一次get请求
   * 自动创建临时Client,请求结束后自动删除
   * 如果要对主机发送多个请求,要手动new Client
   *
   * var client = new Client();
   * client.get(...)
   * client.put(...)
   *
   * 其他请求有:post put patch delete
   * 返回值为Future<Response>
   *
   * Http请求有可能返回Response,或者异常
   */
  String url = "https://api.github.com/repos/square/okhttp/contributors";
  print(url);
   var future = await Http.get(url);
  print(future);
  future.headers.keys.forEach((str)=>print(str));
  print("content-type= ${future.headers['content-type']}");
  /**
   * 获取响应对象的主体内容
   * Response.bodyBytes返回字节List
   *
   * Response.body返回String
   * 但仍然是通过对bodyBytes进行编码来实现
   * 调用Response.body的时候,默认以content-type的字段值来进行编码
   * 比如content-type = text/html; charset=utf-8,则采用UTF8进行编码
   *
   * 如果源码有无效的字符
   * 那么直接调用Response.body的时候出错,会显示错误:
   * FormatException: Bad UTF-8 encoding 0xfd
   * 那么我们就需要手动对字节进行编码
   * 设置allowMalformed为true,对无效或未完成的字符进行替换
   */
   打印状态码: 结果 为 200 
  print("respstatus: ${future.statusCode}");
  /// 打印body
  print("respbody: ${future.body}");
  
 List<int> bytes = future.bodyBytes;
  String utf8Body = utf8.decode(bytes, allowMalformed: true).substring(0, 800);
  print(utf8Body + "\n");

打印结果:

Observatory listening on http://127.0.0.1:54315/Fw1eH7ERn5Y=/
https://api.github.com/repos/square/okhttp/contributors
Instance of 'Response'
cache-control
last-modified
access-control-allow-origin
x-ratelimit-limit
status
transfer-encoding
date
x-ratelimit-reset
content-encoding
vary
strict-transport-security
referrer-policy
access-control-expose-headers
x-ratelimit-remaining
content-type
x-xss-protection
server
link
x-github-request-id
etag
x-frame-options
x-content-type-options
content-security-policy
x-github-media-type
content-type= application/json; charset=utf-8
respstatus: 200
respbody: [{"login":"swankjesse","id":133019,"node_id":"MDQ6VXNlcjEzMzAxOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/133019?v=4","gravatar_id":"","url":"https://api.github.com/users/swankjesse","html_url":"https://github.com/swankjesse","followers_url":"https://api.github.com/users/swankjesse/followers","following_url":"https://api.github.com/users/swankjesse/following{/other_user}","gists_url":"https://api.github.com/users/swankjesse/gists{/gist_id}","starred_url":"https://api.github.com/users/swankjesse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swankjesse/subscriptions","organizations_url":"https://api.github.com/users/swankjesse/orgs","repos_url":"https://api.github.com/users/swankjesse/repos","events_url":"https://api.github.com/users/swankjesse/events{/privacy}","received_events_url":"https://api.github.com/users/swankjesse/received_events","type":"User","site_admin":false,"contributions":2303},{"login":"JakeWharton","id":66577,"node_id":"MDQ6VXNlcjY2NTc3","avatar_url":"https://avatars0.githubusercontent.com/u/66577?v=4","gravatar_id":"","url":"https://api.github.com/users/JakeWharton","html_url":"https://github.com/JakeWharton","followers_url":"https://api.github.com/users/JakeWharton/followers","following_url":"https://api.github.com/users/JakeWharton/following{/other_user}","gists_url":"https://api.github.com/users/JakeWharton/gists{/gist_id}","starred_url":"https://api.github.com/users/JakeWharton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JakeWharton/subscriptions","organizations_url":"https://api.github.com/users/JakeWharton/orgs","repos_url":"https://api.github.com/users/JakeWharton/repos","events_url":"https://api.github.com/users/JakeWharton/events{/privacy}","received_events_url":"https://api.github.com/users/JakeWharton/received_events","type":"User","site_admin":false,"contributions":768},{"login":"adriancole","id":64215,"node_id":"MDQ6VXNlcjY0MjE1","avatar_url":"https://avatars2.githubusercontent.com/u/64215?v=4","gravatar_id":"","url":"https://api.github.com/users/adriancole","html_url":"https://github.com/adriancole","followers_url":"https://api.github.com/users/adriancole/followers","following_url":"https://api.github.com/users/adriancole/following{/other_user}","gists_url":"https://api.github.com/users/adriancole/gists{/gist_id}","starred_url":"https://api.github.com/users/adriancole/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adriancole/subscriptions","organizations_url":"https://api.github.com/users/adriancole/orgs","repos_url":"https://api.github.com/users/adriancole/repos","events_url":"https://api.github.com/users/adriancole/events{/privacy}","received_events_url":"https://api.github.com/users/adriancole/received_events","type":"User","site_admin":false,"contributions":230},{"login":"yschimke","id":231923,"node_id":"MDQ6VXNlcjIzMTkyMw==","avatar_url":"https://avatars1.githubusercontent.com/u/231923?v=4","gravatar_id":"","url":"https://api.github.com/users/yschimke","html_url":"https://github.com/yschimke","followers_url":"https://api.github.com/users/yschimke/followers","following_url":"https://api.github.com/users/yschimke/following{/other_user}","gists_url":"https://api.github.com/users/yschimke/gists{/gist_id}","starred_url":"https://api.github.com/users/yschimke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yschimke/subscriptions","organizations_url":"https://api.github.com/users/yschimke/orgs","repos_url":"https://api.github.com/users/yschimke/repos","events_url":"https://api.github.com/users/yschimke/events{/privacy}","received_events_url":"https://api.github.com/users/yschimke/received_events","type":"User","site_admin":false,"contributions":225},{"login":"amirlivneh","id":724254,"node_id":"MDQ6VXNlcjcyNDI1NA==","avatar_url":"https://avatars0.githubusercontent.com/u/724254?v=4","gravatar_id":"","url":"https://api.github.com/users/amirlivneh","html_url":"https://github.com/amirlivneh","followers_url":"https://api.github.com/users/amirlivneh/followers","following_url":"https://api.github.com/users/amirlivneh/following{/other_user}","gists_url":"https://api.github.com/users/amirlivneh/gists{/gist_id}","starred_url":"https://api.github.com/users/amirlivneh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amirlivneh/subscriptions","organizations_url":"https://api.github.com/users/amirlivneh/orgs","repos_url":"https://api.github.com/users/amirlivneh/repos","events_url":"https://api.github.com/users/amirlivneh/events{/privacy}","received_events_url":"https://api.github.com/users/amirlivneh/received_events","type":"User","site_admin":false,"contributions":72},{"login":"nfuller","id":6388721,"node_id":"MDQ6VXNlcjYzODg3MjE=","avatar_url":"https://avatars3.githubusercontent.com/u/6388721?v=4","gravatar_id":"","url":"https://api.github.com/users/nfuller","html_url":"https://github.com/nfuller","followers_url":"https://api.github.com/users/nfuller/followers","following_url":"https://api.github.com/users/nfuller/following{/other_user}","gists_url":"https://api.github.com/users/nfuller/gists{/gist_id}","starred_url":"https://api.github.com/users/nfuller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nfuller/subscriptions","organizations_url":"https://api.github.com/users/nfuller/orgs","repos_url":"https://api.github.com/users/nfuller/repos","events_url":"https://api.github.com/users/nfuller/events{/privacy}","received_events_url":"https://api.github.com/users/nfuller/received_events","type":"User","site_admin":false,"contributions":56},{"login":"dave-r12","id":1520761,"node_id":"MDQ6VXNlcjE1MjA3NjE=","avatar_url":"https://avatars2.githubusercontent.com/u/1520761?v=4","gravatar_id":"","url":"https://api.github.com/users/dave-r12","html_url":"https://github.com/dave-r12","followers_url":"https://api.github.com/users/dave-r12/followers","following_url":"https://api.github.com/users/dave-r12/following{/other_user}","gists_url":"https://api.github.com/users/dave-r12/gists{/gist_id}","starred_url":"https://api.github.com/users/dave-r12/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dave-r12/subscriptions","organizations_url":"https://api.github.com/users/dave-r12/orgs","repos_url":"https://api.github.com/users/dave-r12/repos","events_url":"https://api.github.com/users/dave-r12/events{/privacy}","received_events_url":"https://api.github.com/users/dave-r12/received_events","type":"User","site_admin":false,"contributions":50},{"login":"mescortes","id":1553664,"node_id":"MDQ6VXNlcjE1NTM2NjQ=","avatar_url":"https://avatars0.githubusercontent.com/u/1553664?v=4","gravatar_id":"","url":"https://api.github.com/users/mescortes","html_url":"https://github.com/mescortes","followers_url":"https://api.github.com/users/mescortes/followers","following_url":"https://api.github.com/users/mescortes/following{/other_user}","gists_url":"https://api.github.com/users/mescortes/gists{/gist_id}","starred_url":"https://api.github.com/users/mescortes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mescortes/subscriptions","organizations_url":"https://api.github.com/users/mescortes/orgs","repos_url":"https://api.github.com/users/mescortes/repos","events_url":"https://api.github.com/users/mescortes/events{/privacy}","received_events_url":"https://api.github.com/users/mescortes/received_events","type":"User","site_admin":false,"contributions":30},{"login":"oldergod","id":1767669,"node_id":"MDQ6VXNlcjE3Njc2Njk=","avatar_url":"https://avatars3.githubusercontent.com/u/1767669?v=4","gravatar_id":"","url":"https://api.github.com/users/oldergod","html_url":"https://github.com/oldergod","followers_url":"https://api.github.com/users/oldergod/followers","following_url":"https://api.github.com/users/oldergod/following{/other_user}","gists_url":"https://api.github.com/users/oldergod/gists{/gist_id}","starred_url":"https://api.github.com/users/oldergod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oldergod/subscriptions","organizations_url":"https://api.github.com/users/oldergod/orgs","repos_url":"https://api.github.com/users/oldergod/repos","events_url":"https://api.github.com/users/oldergod/events{/privacy}","received_events_url":"https://api.github.com/users/oldergod/received_events","type":"User","site_admin":false,"contributions":29},{"login":"NightlyNexus","id":4032667,"node_id":"MDQ6VXNlcjQwMzI2Njc=","avatar_url":"https://avatars2.githubusercontent.com/u/4032667?v=4","gravatar_id":"","url":"https://api.github.com/users/NightlyNexus","html_url":"https://github.com/NightlyNexus","followers_url":"https://api.github.com/users/NightlyNexus/followers","following_url":"https://api.github.com/users/NightlyNexus/following{/other_user}","gists_url":"https://api.github.com/users/NightlyNexus/gists{/gist_id}","starred_url":"https://api.github.com/users/NightlyNexus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NightlyNexus/subscriptions","organizations_url":"https://api.github.com/users/NightlyNexus/orgs","repos_url":"https://api.github.com/users/NightlyNexus/repos","events_url":"https://api.github.com/users/NightlyNexus/events{/privacy}","received_events_url":"https://api.github.com/users/NightlyNexus/received_events","type":"User","site_admin":false,"contributions":19},{"login":"TangHuaiZhe","id":7965577,"node_id":"MDQ6VXNlcjc5NjU1Nzc=","avatar_url":"https://avatars3.githubusercontent.com/u/7965577?v=4","gravatar_id":"","url":"https://api.github.com/users/TangHuaiZhe","html_url":"https://github.com/TangHuaiZhe","followers_url":"https://api.github.com/users/TangHuaiZhe/followers","following_url":"https://api.github.com/users/TangHuaiZhe/following{/other_user}","gists_url":"https://api.github.com/users/TangHuaiZhe/gists{/gist_id}","starred_url":"https://api.github.com/users/TangHuaiZhe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TangHuaiZhe/subscriptions","organizations_url":"https://api.github.com/users/TangHuaiZhe/orgs","repos_url":"https://api.github.com/users/TangHuaiZhe/repos","events_url":"https://api.github.com/users/TangHuaiZhe/events{/privacy}","received_events_url":"https://api.github.com/users/TangHuaiZhe/received_events","type":"User","site_admin":false,"contributions":19},{"login":"ShaishavGandhi","id":2227639,"node_id":"MDQ6VXNlcjIyMjc2Mzk=","avatar_url":"https://avatars1.githubusercontent.com/u/2227639?v=4","gravatar_id":"","url":"https://api.github.com/users/ShaishavGandhi","html_url":"https://github.com/ShaishavGandhi","followers_url":"https://api.github.com/users/ShaishavGandhi/followers","following_url":"https://api.github.com/users/ShaishavGandhi/following{/other_user}","gists_url":"https://api.github.com/users/ShaishavGandhi/gists{/gist_id}","starred_url":"https://api.github.com/users/ShaishavGandhi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ShaishavGandhi/subscriptions","organizations_url":"https://api.github.com/users/ShaishavGandhi/orgs","repos_url":"https://api.github.com/users/ShaishavGandhi/repos","events_url":"https://api.github.com/users/ShaishavGandhi/events{/privacy}","received_events_url":"https://api.github.com/users/ShaishavGandhi/received_events","type":"User","site_admin":false,"contributions":18},{"login":"Egorand","id":1186186,"node_id":"MDQ6VXNlcjExODYxODY=","avatar_url":"https://avatars3.githubusercontent.com/u/1186186?v=4","gravatar_id":"","url":"https://api.github.com/users/Egorand","html_url":"https://github.com/Egorand","followers_url":"https://api.github.com/users/Egorand/followers","following_url":"https://api.github.com/users/Egorand/following{/other_user}","gists_url":"https://api.github.com/users/Egorand/gists{/gist_id}","starred_url":"https://api.github.com/users/Egorand/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Egorand/subscriptions","organizations_url":"https://api.github.com/users/Egorand/orgs","repos_url":"https://api.github.com/users/Egorand/repos","events_url":"https://api.github.com/users/Egorand/events{/privacy}","received_events_url":"https://api.github.com/users/Egorand/received_events","type":"User","site_admin":false,"contributions":14},{"login":"15characterlimi","id":2462356,"node_id":"MDQ6VXNlcjI0NjIzNTY=","avatar_url":"https://avatars1.githubusercontent.com/u/2462356?v=4","gravatar_id":"","url":"https://api.github.com/users/15characterlimi","html_url":"https://github.com/15characterlimi","followers_url":"https://api.github.com/users/15characterlimi/followers","following_url":"https://api.github.com/users/15characterlimi/following{/other_user}","gists_url":"https://api.github.com/users/15characterlimi/gists{/gist_id}","starred_url":"https://api.github.com/users/15characterlimi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/15characterlimi/subscriptions","organizations_url":"https://api.github.com/users/15characterlimi/orgs","repos_url":"https://api.github.com/users/15characterlimi/repos","events_url":"https://api.github.com/users/15characterlimi/events{/privacy}","received_events_url":"https://api.github.com/users/15characterlimi/received_events","type":"User","site_admin":false,"contributions":13},{"login":"monkey-mas","id":5261677,"node_id":"MDQ6VXNlcjUyNjE2Nzc=","avatar_url":"https://avatars2.githubusercontent.com/u/5261677?v=4","gravatar_id":"","url":"https://api.github.com/users/monkey-mas","html_url":"https://github.com/monkey-mas","followers_url":"https://api.github.com/users/monkey-mas/followers","following_url":"https://api.github.com/users/monkey-mas/following{/other_user}","gists_url":"https://api.github.com/users/monkey-mas/gists{/gist_id}","starred_url":"https://api.github.com/users/monkey-mas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/monkey-mas/subscriptions","organizations_url":"https://api.github.com/users/monkey-mas/orgs","repos_url":"https://api.github.com/users/monkey-mas/repos","events_url":"https://api.github.com/users/monkey-mas/events{/privacy}","received_events_url":"https://api.github.com/users/monkey-mas/received_events","type":"User","site_admin":false,"contributions":13},
Observatory listening on http://127.0.0.1:55072/0DJnPKUTiuI=/
[{"login":"swankjesse","id":133019,"node_id":"MDQ6VXNlcjEzMzAxOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/133019?v=4","gravatar_id":"","url":"https://api.github.com/users/swankjesse","html_url":"https://github.com/swankjesse","followers_url":"https://api.github.com/users/swankjesse/followers","following_url":"https://api.github.com/users/swankjesse/following{/other_user}","gists_url":"https://api.github.com/users/swankjesse/gists{/gist_id}","starred_url":"https://api.github.com/users/swankjesse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swankjesse/subscriptions","organizations_url":"https://api.github.com/users/swankjesse/orgs","repos_url":"https://api.github.com/users/swankjesse/repos","events_url":"https://api.github.com/users/swankjesse/ev

上边打印了square okhttp 的contributors 的json 所有内容。

获取响应的头文件

  var url = 'http://httpbin.org/';
  Http.get(url).then((response) {
    //获取头文件的映射
    print(response.headers.keys);
    //获取头文件的信息
    print("access-control-allow-origin = ${response.headers['access-control-allow-origin']}");
    print("content-type = ${response.headers['content-type']}");
    print("date = ${response.headers['date']}");
    print("content-length = ${response.headers['content-length']}");
    print("connection = ${response.headers['connection']}");
  });

打印结果:

Observatory listening on http://127.0.0.1:55015/yTTc96Zf4eI=/
(connection, access-control-allow-credentials, access-control-allow-origin, ..., x-content-type-options, server)
access-control-allow-origin = *
content-type = text/html; charset=utf-8
date = Sat, 14 Sep 2019 10:36:04 GMT
content-length = 3168
connection = keep-alive

下边Http.read 直接读取 get 请求,但是不返回response 我们可以直接截取内容。

  /**
   * read和readBytes作用为向主机发送get请求,但不返回Response
   * 返回值为Future<String>和Future<Uint8List>
   */
  print((await Http.read("https://api.github.com/repos/square/okhttp/contributors")).substring(0, 100) + "\n");

打印结果:

Observatory listening on http://127.0.0.1:54405/dRtBnNFZ7ko=/
[{"login":"swankjesse","id":133019,"node_id":"MDQ6VXNlcjEzMzAxOQ==","avatar_url":"https://avatars3.g

修改请求头

/**
   * 设置请求的标签头
   * 通过修改User-Agent来模拟iPhone 6请求
   * 显示手机版百度首页
   *
   * 可以对比之前read的字符串
   */

  Http.get('https://api.github.com/repos/square/okhttp/contributors',
      headers: {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'}).then((resp) {
    print(resp.body.substring(0, 1000));
  });

打印结果:

Observatory listening on http://127.0.0.1:54416/HTkVMTkLwsk=/
[
  {
    "login": "swankjesse",
    "id": 133019,
    "node_id": "MDQ6VXNlcjEzMzAxOQ==",
    "avatar_url": "https://avatars3.githubusercontent.com/u/133019?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/swankjesse",
    "html_url": "https://github.com/swankjesse",
    "followers_url": "https://api.github.com/users/swankjesse/followers",
    "following_url": "https://api.github.com/users/swankjesse/following{/other_user}",
    "gists_url": "https://api.github.com/users/swankjesse/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/swankjesse/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/swankjesse/subscriptions",
    "organizations_url": "https://api.github.com/users/swankjesse/orgs",
    "repos_url": "https://api.github.com/users/swankjesse/repos",
    "events_url": "https://api.github.com/users/swankjesse/events{/privacy}",
    "received_events_url": "https://api.github.com/users/swankjesse/received_events",
      

post 请求

请求方式 , 举个栗子,

  var url = 'http://httpbin.org/post';
  //向指定URL发送带参数的POST请求
  Http.post(url ,body: 'name=dartlang&color=red').then((response) {
    print("响应状态: ${response.statusCode}");
    print("响应正文: ${response.body}");
  });

打印结果:

Observatory listening on http://127.0.0.1:54429/iaZcMODrXVw=/
响应状态: 200
响应正文: {
  "args": {}, 
  "data": "name=dartlang&color=red", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "23", 
    "Content-Type": "text/plain; charset=utf-8", 
    "Host": "httpbin.org", 
    "User-Agent": "Dart/2.4 (dart:io)"
  }, 
  "json": null, 
  "origin": "1.202.192.6, 1.202.192.6", 
  "url": "https://httpbin.org/post"
}

Json 展示

    var url = 'https://api.github.com/users/dart-lang/repos';
    Http.get(url ,headers : {'User-Agent':'Dart/2.4.0(My Dart client)'}).then((response) {
      //解析响应字符串,并生成Json对象
      List<dynamic> repos = jsonDecode(response.body);
      //输出说明字符串
      var heading = 'Repository | Star count | Fork count';
      print(heading);
      //输出与说明字符串等长的‘=’字符串
      print(new List.filled(heading.length, '=').join());
      //循环输出Json对象的指定参数
      for (var repo in repos) {

        print("${repo['name']} | ${repo['stargazers_count']}  | ${repo['forks_count']}");

      }
    });

打印结果:

Observatory listening on http://127.0.0.1:54722/CMzqClJLO4E=/
Repository | Star count | Fork count
====================================
.github | 1  | 0
angular | 1242  | 167
angular_components | 365  | 88
api.dart.dev | 9  | 11
appengine | 66  | 23
appengine_samples | 28  | 11
args | 51  | 29
async | 33  | 19
bazel_worker | 10  | 7
benchmark_harness | 28  | 15
bolt | 0  | 0
boolean_selector | 3  | 7
boringssl_gen | 0  | 5
browser_launcher | 5  | 4
build | 256  | 73
characters | 0  | 1
charcode | 13  | 4
chocolatey-packages | 1  | 0
cli_util | 16  | 7
clock | 3  | 1
co19 | 8  | 19
code_builder | 101  | 23
collection | 53  | 20
convert | 13  | 10
coverage | 51  | 37
crypto | 70  | 18
csslib | 12  | 14
dart-doc-syncer | 6  | 6
dart-pad | 260  | 75
dart-services | 91  | 36

是不是很简单,随和。

向服务器发多个请求

/*
输出响应正文
 */
printResponseBody(response) {
  //输出响应正文的长度
  print(response.body.length);
  //控制输出的长度在100以内
  if (response.body.length > 200) {
    print(response.body.substring(0, 200));
  } else {
    print(response.body);
  }
  print('...\n');
}


  var url = 'http://www.baidu.com/';
  //Client类,保持持久连接,向同一个服务器发多个请求
  var client = new Http.Client();
  //第一次请求/search
  client.get('${url}/s?wd=okhttp').then((response) {
    printResponseBody(response);
    //第二次请求/doodles
    return client.get('${url}/s?wd=cat');
  }).then(printResponseBody)
  //完成后关闭Client连接
   .whenComplete(client.close);

打印结果:

Observatory listening on http://127.0.0.1:54870/guC3XuHXKnw=/
292955
<!DOCTYPE html>
<!--STATUS OK-->
<html>
	<head>
		<meta
...
326680
<!DOCTYPE html>

处理请求错误


  Http.get("http://www.ddd.com")
    ..then(handleSuccess)
    ..catchError(handleFailure);


//访问正确时的操作
handleSuccess(Http.Response response) {
  print('成功访问了网站!');
  print(response.body);
}

//访问错误时的操作
handleFailure(error) {
  print('发生了一些错误!');
  print(error.message);
}

打印结果:

Observatory listening on http://127.0.0.1:54929/VIVZVj8IHCE=/
发生了一些错误!
Failed host lookup: 'www.ddd.com'
Unhandled exception:
SocketException: Failed host lookup: 'www.ddd.com' (OS Error: nodename nor servname provided, or not known, errno = 8)
#0      IOClient.send (package:http/src/io_client.dart:33:23)
<asynchronous suspension>
#1      BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
<asynchronous suspension>
#2      BaseClient.get (package:http/src/base_client.dart:32:7)
#3      get.<anonymous closure> (package:http/http.dart:46:36)
#4      _withClient (package:http/http.dart:166:20)
<asynchronous suspension>
#5      get (package:http/http.dart:46:5)
#6      main (file:///learndev/dartdev/DartTest/bin/DartHttp.dart:152:3)
<asynchronous suspension>
#7      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#8      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)

先到这里吧 ,如有问题欢迎评论区留言,我们共同讨论学习进步。 溜娃去了, 拜拜

参考资料

  1. http://www.cndartlang.com
  2. https://www.jianshu.com/p/0dbb26f58eb4
  3. https://blog.csdn.net/hekaiyou/article/details/46521756
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值