08-Flutter mobile commercially practical basis -dio data acquisition request header _ forge

In many cases, the back-end for safety, there will be some restrictions on request headers, only a request for a head, to return the correct data. While this limits the number of malicious request data, but our clever programmers, it is useless. Take this article geeks time, for example, by forging a request to talk about the head, time to get Geeks Home key data. (No guarantee interfaces and security measures have been available Oh, hurry to practice it)

View geeks time data ports

If you are a front-end, this process may already know by heart, to find a port Denver, for analysis.

First, open the browser side Nuggets website (I use chrome browser :: https is: //time.geekbang.org/, and then press F12 to open the browser console, came NetWork tab, then select XHR tab this page will refresh time asynchronous data requests occur. we chose newAll this interface to view it.

Copy URL: https: //time.geekbang.org/serv/v1/column/newAll

We are with this interface as an example, to retrieve its data.

Illegal request implementation

With the interface, we put the lesson pages about the transformation. Note that, at this time we do not set the request header, in order to demonstrate that we do not configure request header is unable to obtain the data, it will return a 451 error.

451: is an illegal request, your request is not legitimate, pull off the server request, we have nothing to return to.

code show as below:

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';


class HomePage extends StatefulWidget {
  _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)
             ],
           ),
         ),
       ),
    );
  }

  void _jike(){
    print('开始向极客时间请求数据..................');
    getHttp().then((val){
      setState(() {
       showText=val['data'].toString();
      });
    });
  }


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

}

Preview this time we will return to the current console output ruthless exception message.

I/flutter ( 6942): DioError [DioErrorType.RESPONSE]: Http status error [451]
E/flutter ( 6942): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception
Request header forgery

Create a new folder, named called config, and then create a file in it httpHeaders.dart, set the request header, request header can easily get in the browser, the need for reform after obtaining.

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'
};

With the request header, the file may be modified body, modify the request header is introduced, and is provided, this two main code.

import '../config/httpHeaders.dart';
dio.options.headers= httpHeaders;

All code is as follows:

import 'package:flutter/material.dart';
import '../service/service_method.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage{

  String homePageContent = "正在获取数据...";

  @override
  void initState() {
    getHomePageContent().then((val){
      setState(() {
        homePageContent = val.toString();
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: new AppBar(title: Text("百姓生活"),),
        body: SingleChildScrollView(
          child: Text(homePageContent),
        ),
      ),
    );
  }

}

Now you can get the normal data. FIG effect is as follows:

Lessons Learned:

This section focuses on learning how to Dio others head to get data interface, I learned this is very useful by forging a request, since we want to make their own during practice Demo will not have to worry for the back-end interfaces.

Guess you like

Origin www.cnblogs.com/niceyoo/p/11029127.html