当前位置: 首页 > article >正文

charles抓包flutter

一,准备工作

在我的另一篇文章flutter Dio发送post请求-CSDN博客里面,直接复用一部分代码

该方法无需让手机安装charles的ca证书(当然安装了也没事儿),也无需设置手机wifi的网络代理(因为ca证书的内容和网络代理都写进dart代码中了)

注:需要先打开charles作为代理,手机和电脑在同一局域网中(或者电脑or手机一个开热点给另一个连上)

二,coding

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:dio/adapter.dart';

final Dio dio = Dio();

class AcceptCA extends HttpOverrides {
  // 信任证书
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    SecurityContext securityContext = SecurityContext(withTrustedRoots: true);
    // ca证书的内容
    var caContent = Uint8List.fromList(utf8.encode('''-----BEGIN CERTIFICATE-----
MIIFKjCCBBKgAwIBAgIGAZIDF60vMA0GCSqGSIb3DQEBCwUAMIGZMSowKAYDVQQDDCFDaGFybGVz
IFByb3h5IENBICgxOCBTZXAgMjAyNCwgTCkxJTAjBgNVBAsMHGh0dHBzOi8vY2hhcmxlc3Byb3h5
LmNvbS9zc2wxETAPBgNVBAoMCFhLNzIgTHRkMREwDwYDVQQHDAhBdWNrbGFuZDERMA8GA1UECAwI
QXVja2xhbmQxCzAJBgNVBAYTAk5aMB4XDTI0MDkxNzAzMDUzNloXDTI1MDkxNzAzMDUzNlowgZkx
KjAoBgNVBAMMIUNoYXJsZXMgUHJveHkgQ0EgKDE4IFNlcCAyMDI0LCBMKTElMCMGA1UECwwcaHR0
cHM6Ly9jaGFybGVzcHJveHkuY29tL3NzbDERMA8GA1UECgwIWEs3MiBMdGQxETAPBgNVBAcMCEF1
Y2tsYW5kMREwDwYDVQQIDAhBdWNrbGFuZDELMAkGA1UEBhMCTlowggEiMA0GCSqGSIb3DQEBAQUA
A4IBDwAwggEKAoIBAQC7B4T2grKCDC0SH/W9v+lbZMYL9R4d34Xhe/g4l+MC38GYfq4oonIprMbb
wNpeSvlrriDeImp1/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dbxT3aZSoFxkCaiGAh4LHmZ5vogm9OTc2hOzDssUbrTxyLFBwzXKQlHBTQ
bDi7/SxEYgwQ2XVEDBm/
-----END CERTIFICATE-----'''));
    securityContext.setTrustedCertificatesBytes(caContent);
    return super.createHttpClient(securityContext);
  }
}

main() {
  runApp(const MaterialApp(
    home: H(),
  ));
}

class H extends StatefulWidget {
  const H({super.key});

  @override
  State<H> createState() => _HState();
}

class _HState extends State<H> {
  String _resStr = "";

  @override
  void initState() {
    super.initState();
    HttpOverrides.global = AcceptCA();
    // 设置网络代理
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
        (HttpClient client) {
      client.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
      client.findProxy = (uri) => 'PROXY 192.168.111.111:8888';
    };
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("抓包测试"),
        ),
        body: Center(
          child: Text(
            _resStr,
            style: const TextStyle(fontSize: 30),
          ),
        ),
        floatingActionButton:
            FloatingActionButton(onPressed: _postRequestFunction));
  }

  void _postRequestFunction() async {
    FormData formData = FormData.fromMap({"sourceText": "Who are you?"});
    String url = "https://api.oioweb.cn/api/txt/QQFanyi";
    Response response = await dio.post(url, data: formData);
    setState(() {
      _resStr =
          json.decode(response.toString())["result"]["targetText"].toString();
    });
  }
}


http://www.kler.cn/a/314417.html

相关文章:

  • [运维][Nginx]Nginx学习(1/5)--Nginx基础
  • Fastapi使用MongoDB作为数据库
  • 前端常用布局模板39套,纯CSS实现布局
  • AcWing 302 任务安排 斜率优化的dp
  • Ubuntu安装MySQL8
  • 《情商》提升:增强自我意识,学会与情绪共处
  • 数据结构之线性表——LeetCode:328. 奇偶链表,86. 分隔链表,24. 两两交换链表中的节点
  • 基于React+JsonServer+Antddesign的读书笔记管理系统
  • 4.使用 VSCode 过程中的英语积累 - View 菜单(每一次重点积累 5 个单词)
  • 微软AI核电计划
  • SpringBoot 项目启动时指定外部配置文件
  • 【Android 13源码分析】WindowContainer窗口层级-4-Layer树
  • Android通知显示framework流程解析
  • Python中的魔法:栈与队列的奇妙之旅
  • 大语言模型的发展-OPENBMB
  • ICM20948 DMP代码详解(34)
  • 欧美游戏市场的差异
  • 漏洞复现_永恒之蓝
  • AI助力低代码平台:从智能化到高效交付的全新变革
  • 山体滑坡检测系统源码分享
  • STM32 通过 SPI 驱动 W25Q128
  • 【JS】垃圾回收机制与内存泄漏
  • mxnet 的显存分配机制
  • Gitlab学习(009 gitlab冲突提交)
  • 小程序与APP的区别
  • 大数据-137 - ClickHouse 集群 表引擎详解2 - MergeTree 存储结构 一级索引 跳数索引