美文网首页
Dio 伪造请求头,获取别人网站的接口

Dio 伪造请求头,获取别人网站的接口

作者: 叶秋_YQ | 来源:发表于2019-05-08 16:23 被阅读0次
    效果演示
    import 'package:flutter/material.dart';
    import 'package:dio/dio.dart';
    import '../config/http_headers.dart';
    
    class HomePage extends StatefulWidget {
      HomePage({Key key}) : super(key: key);
    
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      String showText = '还没有请求数据';
      @override
      Widget build(BuildContext context) {
        return Container(
           child: Scaffold(
             appBar: AppBar(title: Text('请求远程数据')),
             body: SingleChildScrollView(
               child: Column(
                 children: <Widget>[
                   RaisedButton(
                     onPressed: _jike,
                     child: Text('请求数据'),
                   ),
                   Text(showText)
                 ],
               ),
             ),
           )
        );
      }
    
      _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){
          print(e);
        }
      }
    }
    

    网站的请求头

    // 打开网站F12里面复制过来
    const httpHeaders = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json',
    'Cookie': '_ga=GA1.2.1213330956.1557299866; _gid=GA1.2.1546145805.1557299866; Hm_lvt_022f847c4e3acd44d4a2481d9187f1e6=1557299866; SERVERID=3431a294a18c59fc8f5805662e2bd51e|1557300893|1557299841; _gat=1; Hm_lpvt_022f847c4e3acd44d4a2481d9187f1e6=1557300917',
    'Host': 'time.geekbang.org',
    'Origin': 'https://time.geekbang.org',
    'Referer': 'https://time.geekbang.org/',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
    };
    

    相关文章

      网友评论

          本文标题:Dio 伪造请求头,获取别人网站的接口

          本文链接:https://www.haomeiwen.com/subject/kghvoqtx.html