|
1 | 1 | import 'dart:developer'; |
2 | 2 | import 'package:get/get.dart'; |
3 | | -import 'package:http/http.dart' as http; |
| 3 | +import 'package:dio/dio.dart'; |
4 | 4 | import 'package:pure_live/common/index.dart'; |
5 | 5 | import 'package:waterfall_flow/waterfall_flow.dart'; |
6 | 6 | import 'package:pure_live/routes/app_navigation.dart'; |
@@ -65,38 +65,33 @@ class OwnerCard extends StatefulWidget { |
65 | 65 | class _OwnerCardState extends State<OwnerCard> { |
66 | 66 | SettingsService settings = Get.find<SettingsService>(); |
67 | 67 |
|
68 | | - Future<String?> getFinalUrl(String requestUrl) async { |
| 68 | + Future<String?> getFinalUrlWithDio(String requestUrl) async { |
| 69 | + final dio = Dio(); |
69 | 70 | try { |
70 | | - final url = Uri.parse(requestUrl); |
71 | | - final client = http.Client(); |
72 | | - log("requestUrl: $requestUrl"); |
73 | | - // 1. 发送请求 |
74 | | - final request = http.Request('GET', url)..followRedirects = false; |
75 | | - final response = await client.send(request); |
76 | | - log("Status Code: ${response.statusCode}"); |
77 | | - // 2. 获取重定向地址 |
78 | | - final redirectUrl = response.headers['location']; |
79 | | - log("roomId is 0, skip $redirectUrl"); |
80 | | - if (redirectUrl == null || redirectUrl.isEmpty) { |
81 | | - return null; |
82 | | - } |
83 | | - final segments = Uri.parse(redirectUrl).pathSegments; |
84 | | - if (segments.isNotEmpty) { |
85 | | - return segments.last; |
| 71 | + final response = await dio.get( |
| 72 | + requestUrl, |
| 73 | + options: Options( |
| 74 | + followRedirects: false, // 禁止自动跳转 |
| 75 | + validateStatus: (status) => status! < 500, |
| 76 | + ), |
| 77 | + ); |
| 78 | + final redirectUrl = response.headers.value('location'); |
| 79 | + if (redirectUrl != null) { |
| 80 | + return Uri.parse(redirectUrl).pathSegments.lastOrNull; |
86 | 81 | } |
87 | | - |
88 | 82 | return null; |
89 | 83 | } catch (e) { |
90 | | - log("Error occurred: $e"); |
91 | 84 | return null; |
| 85 | + } finally { |
| 86 | + dio.close(); // 释放资源 |
92 | 87 | } |
93 | 88 | } |
94 | 89 |
|
95 | 90 | void _onTap(BuildContext context) async { |
96 | 91 | String roomId = widget.room.roomId!; |
97 | 92 | if (widget.room.platform == Sites.huyaSite) { |
98 | 93 | if (widget.room.roomId == '0') { |
99 | | - var roomIdInfo = await getFinalUrl('https://www.huya.com/yy/${widget.room.userId!}'); |
| 94 | + var roomIdInfo = await getFinalUrlWithDio('https://www.huya.com/yy/${widget.room.userId!}'); |
100 | 95 | if (roomIdInfo != null) { |
101 | 96 | roomId = roomIdInfo; |
102 | 97 | } |
|
0 commit comments