flutter 网络请求头
Posted pp-pping
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flutter 网络请求头相关的知识,希望对你有一定的参考价值。
代码头:
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 网络请求头的主要内容,如果未能解决你的问题,请参考以下文章