码迷,mamicode.com
首页 > 其他好文 > 详细

flutter 网络请求头

时间:2020-01-21 16:11:16      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:idg   try   encoding   margin   ica   xtend   org   safari   option   

代码头:


const HttpHeaders = {
‘Accept‘: ‘application/json, text/plain, */*‘,
‘Accept-Encoding‘: ‘gzip, deflate, br‘,
‘Accept-Language‘: ‘zh-CN,zh;q=0.9‘,
‘Connection‘: ‘keep-alive‘,
‘Content-Type‘: ‘application/json‘,
‘Cookie‘: ‘_ga=GA1.2.676402787.1548321037; GCID=9d149c5-11cb3b3-80ad198-04b551d; _gid=GA1.2.359074521.1550799897; _gat=1; Hm_lvt_022f847c4e3acd44d4a2481d9187f1e6=1550106367,1550115714,1550123110,1550799897; SERVERID=1fa1f330efedec1559b3abbcb6e30f50|1550799909|1550799898; Hm_lpvt_022f847c4e3acd44d4a2481d9187f1e6=1550799907‘,
‘Host‘: ‘time.geekbang.org‘,
‘Origin‘: ‘https://time.geekbang.org‘,
‘Referer‘: ‘https://time.geekbang.org/‘,
‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36‘
};
测试代码:
import ‘dart:io‘;

import ‘package:dio/dio.dart‘;
import ‘package:flutter/material.dart‘;
import ‘package:flutter_project/config/httpheaders.dart‘;
class NetWorkDemo extends StatefulWidget {
NetWorkDemo({Key key}) : super(key: key);

@override
_NetWorkDemoState createState() => _NetWorkDemoState();
}

class _NetWorkDemoState extends State<NetWorkDemo> {

String _showText = ‘xxxx‘;
@override
Widget build(BuildContext context) {
return Container(
child: Scaffold(
appBar: AppBar(title: Text(‘请求头‘)),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
RaisedButton(
onPressed: (){

_testJike();
},
child: Text(‘请求数据‘),

),
Text(_showText)
],
),
),
),
);
}
void _testJike(){
print(‘请求数据开始‘);
getHttp().then((val){
setState(() {
_showText = val[‘data‘].toString();
});
});

}

Future getHttp() async{
try {
Response res;
Dio dio = new Dio();
dio.options.headers = HttpHeaders;
res = await dio.get("https://time.geekbang.org/serv/v1/column/newAll");
 
print(res);
return res.data;
} catch (e) {
return print(e);
}

}
}
总结:
 

dio.options.headers = xx

flutter 网络请求头

标签:idg   try   encoding   margin   ica   xtend   org   safari   option   

原文地址:https://www.cnblogs.com/pp-pping/p/12221865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!