Skip to content

Commit 1e62d06

Browse files
author
liuchuancong
committed
fix(修复http 卡死)
1 parent 20e8044 commit 1e62d06

2 files changed

Lines changed: 22 additions & 27 deletions

File tree

.vscode/launch.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"name": "Flutter with Proxy",
66
"request": "launch",
77
"type": "dart",
8-
"env": {
9-
"http_proxy": "http://127.0.0.1:7890",
10-
"https_proxy": "http://127.0.0.1:7890",
11-
"HTTP_PROXY": "http://127.0.0.1:7890",
12-
"HTTPS_PROXY": "http://127.0.0.1:7890"
13-
}
8+
// "env": {
9+
// "http_proxy": "http://127.0.0.1:7890",
10+
// "https_proxy": "http://127.0.0.1:7890",
11+
// "HTTP_PROXY": "http://127.0.0.1:7890",
12+
// "HTTPS_PROXY": "http://127.0.0.1:7890"
13+
// }
1414
}
1515
]
1616
}

lib/modules/search/search_list_view.dart

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'dart:developer';
22
import 'package:get/get.dart';
3-
import 'package:http/http.dart' as http;
3+
import 'package:dio/dio.dart';
44
import 'package:pure_live/common/index.dart';
55
import 'package:waterfall_flow/waterfall_flow.dart';
66
import 'package:pure_live/routes/app_navigation.dart';
@@ -65,38 +65,33 @@ class OwnerCard extends StatefulWidget {
6565
class _OwnerCardState extends State<OwnerCard> {
6666
SettingsService settings = Get.find<SettingsService>();
6767

68-
Future<String?> getFinalUrl(String requestUrl) async {
68+
Future<String?> getFinalUrlWithDio(String requestUrl) async {
69+
final dio = Dio();
6970
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;
8681
}
87-
8882
return null;
8983
} catch (e) {
90-
log("Error occurred: $e");
9184
return null;
85+
} finally {
86+
dio.close(); // 释放资源
9287
}
9388
}
9489

9590
void _onTap(BuildContext context) async {
9691
String roomId = widget.room.roomId!;
9792
if (widget.room.platform == Sites.huyaSite) {
9893
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!}');
10095
if (roomIdInfo != null) {
10196
roomId = roomIdInfo;
10297
}

0 commit comments

Comments
 (0)