3.伪造请求头获取数据
2019-12-23 本文已影响0人
冰点雨
请求头代码
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'
};
ea8d9409e260530952458de7bf161e2.png
全部代码
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import '../config/httpHeaders.dart';
class HomePage extends StatefulWidget {
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage>{
TextEditingController typeController = TextEditingController();
String showText = '欢迎你来到美好人间';
@override
Widget build(BuildContext context){
return Container(
child: Scaffold(
appBar: AppBar(title: Text('9999'),),
body: SingleChildScrollView(
child: Container(
height: 1000,
child: Column(
children: <Widget>[
RaisedButton(
onPressed: _jike,
child: Text('请求数据'),
),
Text(showText)
],
),
),
)
),
);
}
void _jike(){
print('开始向极客时间请求数据..................');
getHttp().then((val){
setState(() {
showText=val['data'].toString();
});
});
}
Future getHttp()async{
try{
Response response;
Dio dio = new Dio();
dio.options.headers = httpHeaders;
response =await dio.get("https://time.geekbang.org/serv/v1/column/newAll");
print(response);
return response.data;
}catch(e){
return print(e);
}
}
}