Dart的HTTP请求和响应(2)
来源:互联网 发布:java gzip 解压 编辑:程序博客网 时间:2024/06/15 20:08
向服务器发多个请求
import 'package:http/http.dart' as http;//输出响应正文printResponseBody(response) { //输出响应正文的长度 print(response.body.length); //控制输出的长度在100以内 if (response.body.length > 100) { print(response.body.substring(0, 100)); } else { print(response.body); } print('...\n');}main(List<String> arguments) { var url = 'http://www.google.com/'; //Client类,保持持久连接,向同一个服务器发多个请求 var client = new http.Client(); //第一次请求/search client.get('${url}/search').then((response) { printResponseBody(response); //第二次请求/doodles return client.get('${url}/doodles'); }) .then(printResponseBody) //完成后关闭Client连接 .whenComplete(client.close);}
代码大概意思是,与服务器保持连接的情况下发两次不同的请求,效果如下
处理请求错误
import 'package:http/http.dart' as http;//访问正确时的操作handleSuccess(http.Response response) { print('成功访问了网站!'); print(response.body);}//访问错误时的操作handleFailure(error) { print('发生了一些错误!'); print(error.message);}main(List<String> arguments) { http.get("http://some_bogus_website.org") ..then(handleSuccess) ..catchError(handleFailure);}
因为代码中的请求地址是无效的,所以一定会出错,然后输出指定的错误信息
获取重定向的记录
import "dart:io" show HttpClient, RedirectInfo;main(List<String> arguments) { var client = new HttpClient(); //使用GET方法打开一个HTTP连接 client.getUrl(Uri.parse('http://google.com')) .then((request) => request.close()) .then((response) { //获得所有重定向的列表 List<RedirectInfo> redirects = response.redirects; //循环输出重定向的记录 redirects.forEach((redirect) { print(redirect.location); }); });}
访问代码中的地址时只重定向过一次,所以只有一条记录
获取响应正文的字符串
import 'package:http/http.dart' as http;main(List<String> arguments) { http.read("http://www.google.com/").then(print);}
这个简单得不行,直接看一下效果吧
获取响应正文的二进制
这个要用到crypto包,还是先把分享放出来http://pan.baidu.com/s/1dDARcrB
import 'package:http/http.dart' as http;import 'package:crypto/crypto.dart';main(List<String> arguments) { var url = "https://www.dartlang.org/logos/dart-logo.png"; http.get(url).then((response) { //将响应内容转换成字节列表 List<int> bytes = response.bodyBytes; //将字节列表转换成Base64编码的字符串 String base64 = CryptoUtils.bytesToBase64(bytes); print(base64); });}
因为Base64编码的字符串太长了,我就复制在记事本上了
获取响应的头文件
import 'package:http/http.dart' as http;main(List<String> arguments) { 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']}"); });}
执行代码后的效果如下
0 0
- Dart的HTTP请求和响应(2)
- Dart的HTTP请求和响应(1)
- HTTP的请求和响应
- HTTP和HTTPS的请求和响应
- HTTP请求和响应。
- Http请求和响应
- HTTP请求和响应
- HTTP请求和响应
- Http请求和响应
- HTTP请求和响应
- HTTP请求和响应
- HTTP请求和响应
- http请求和响应
- http请求和响应
- HTTP请求和响应
- HTTP的请求和响应详解
- HTTP的请求和响应详解(转)
- HTTP协议的请求和响应信息
- 虚拟地址和物理地址的概念
- 俄罗斯方块(详解)
- 【HTTP】HTTP报文详解
- Android内存优化之内存缓存
- 没有比脚更长的路,没有比人更高的山
- Dart的HTTP请求和响应(2)
- 【chrome】Chrome源码剖析、上--多线程模型、进程通信、进程模型
- String to Integer (atoi)
- php学习整理之mysql(二)存储引擎、字符集、校对集
- 【Chrome】chrome架构及特性
- Paxos算法
- ~二分答案~
- 4 公众平台-开发文档
- 虚拟内存管理 --嵌入式Linux系统工程师系列之ARM920T的MMU与Cache