flutter 网络请求dio的简单使用以及请求头参数的自定义

dio的使用方式有很多,我就只选出我认为最好用的api方式做下记录,把get成post就是post请求了,网络请求都用的百度的api,实际上的response没有任何意义,所以只要打印出response有值即可。
1.最简单的请求例子,网络请求是异步的所以用async await
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:io';

void main() async{
  Dio dio = Dio();
  final response = await dio.get("https://www.baidu.com/");
  print(response);
}

2.带有参数的get请求

void main() async{

  Map paras = {"wd":"帅气的阿斌"};
  
  Dio dio = Dio();
  final response = await dio.get(
      "https://www.baidu.com/s",
      queryParameters: paras);
  print(response);
}
3.自定义请求头,可定义的请求头dart已经为我们提供了专门的类存了对应的字符,引入以下库,就能使用HttpHeaders

import 'dart:io';
一般我们请求接收到的数据是json格式,如'accept: application/json',我们就可以这样自定义请求头

void main() async{

  Options options = Options(headers: {HttpHeaders.acceptHeader:"accept: application/json"});

  Map paras = {"wd":"帅气的阿斌"};
  
  Dio dio = Dio();
  final response = await dio.get(
      "https://www.baidu.com/s",
      queryParameters: paras,
      options: options);
  print(response);
}
4.使用Baseoptions

                              BaseOptions options = BaseOptions(
                                  method: "get",
                                  baseUrl: "https://www.baidu.com",
                                  queryParameters: {
                                    "areaCode": "",
                                    "cateId":
                                        "1e4bbe70-82d9-48a4-b01c-c61cb6d9096c",
                                    "wd":"帅气的阿斌"
                                  },
                                  headers: {
                                    HttpHeaders.acceptHeader: "*"
                                  });
                              Dio netRequest = Dio(options);
                              final response = await netRequest.get('/s);

                              print(response);
其他详细参数设置参考如下:

abstract class HttpHeaders {
  static const acceptHeader = "accept";
  static const acceptCharsetHeader = "accept-charset";
  static const acceptEncodingHeader = "accept-encoding";
  static const acceptLanguageHeader = "accept-language";
  static const acceptRangesHeader = "accept-ranges";
  static const ageHeader = "age";
  static const allowHeader = "allow";
  static const authorizationHeader = "authorization";
  static const cacheControlHeader = "cache-control";
  static const connectionHeader = "connection";
  static const contentEncodingHeader = "content-encoding";
  static const contentLanguageHeader = "content-language";
  static const contentLengthHeader = "content-length";
  static const contentLocationHeader = "content-location";
  static const contentMD5Header = "content-md5";
  static const contentRangeHeader = "content-range";
  static const contentTypeHeader = "content-type";
  static const dateHeader = "date";
  static const etagHeader = "etag";
  static const expectHeader = "expect";
  static const expiresHeader = "expires";
  static const fromHeader = "from";
  static const hostHeader = "host";
  static const ifMatchHeader = "if-match";
  static const ifModifiedSinceHeader = "if-modified-since";
  static const ifNoneMatchHeader = "if-none-match";
  static const ifRangeHeader = "if-range";
  static const ifUnmodifiedSinceHeader = "if-unmodified-since";
  static const lastModifiedHeader = "last-modified";
  static const locationHeader = "location";
  static const maxForwardsHeader = "max-forwards";
  static const pragmaHeader = "pragma";
  static const proxyAuthenticateHeader = "proxy-authenticate";
  static const proxyAuthorizationHeader = "proxy-authorization";
  static const rangeHeader = "range";
  static const refererHeader = "referer";
  static const retryAfterHeader = "retry-after";
  static const serverHeader = "server";
  static const teHeader = "te";
  static const trailerHeader = "trailer";
  static const transferEncodingHeader = "transfer-encoding";
  static const upgradeHeader = "upgrade";
  static const userAgentHeader = "user-agent";
  static const varyHeader = "vary";
  static const viaHeader = "via";
  static const warningHeader = "warning";
  static const wwwAuthenticateHeader = "www-authenticate";

本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《flutter 网络请求dio的简单使用以及请求头参数的自定义
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(1)

Hello. I have checked your flutterj.com and i see you've got some duplicate
content so probably it is the reason that you don't rank hi in google.
But you can fix this issue fast. There is a tool that rewrites content like human, just search in google:  miftolo's tools
IQ 8个月前 (2019-08-24) 回复