opt: post redirect (#788)

* opt: cookie

* opt: post redirect
This commit is contained in:
My-Responsitories
2025-05-01 10:08:48 +08:00
committed by GitHub
parent a46bde68f5
commit c090cae1a1
5 changed files with 68 additions and 67 deletions

View File

@@ -164,10 +164,8 @@ class Request {
: http11Adapter;
// 先于其他Interceptor
if (GStorage.retryCount > 0) {
dio.interceptors
.add(RetryInterceptor(GStorage.retryCount, GStorage.retryDelay));
}
// 日志拦截器 输出请求、响应内容
if (BuildConfig.isDebug) {
@@ -222,7 +220,7 @@ class Request {
* post请求
*/
Future<Response> post(url,
{data, queryParameters, options, cancelToken, isRedirect}) async {
{data, queryParameters, options, cancelToken}) async {
// debugPrint('post-data: $data');
Response response;
try {
@@ -236,20 +234,6 @@ class Request {
// debugPrint('post success: ${response.data}');
return response;
} on DioException catch (e) {
if (isRedirect != true &&
const [301, 302, 303, 307, 308].contains(e.response?.statusCode)) {
String? redirectUrl = e.response?.headers['location']?.firstOrNull;
if (redirectUrl != null) {
return await post(
redirectUrl,
data: data,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
isRedirect: true,
);
}
}
AccountManager.toast(e);
Response errResponse = Response(
data: {

View File

@@ -35,10 +35,8 @@ class LiveHttp {
'origin': 'https://live.bilibili.com',
'referer': 'https://live.bilibili.com/',
'user-agent': Request.headerUa(type: 'pc'),
if (gaiaVtoken == null) 'cookie': 'x-bili-gaia-vtoken=$gaiaVtoken'
},
extra: gaiaVtoken == null
? null
: {'cookie': 'x-bili-gaia-vtoken=$gaiaVtoken'},
),
);
if (res.data['code'] == 0) {

View File

@@ -10,7 +10,35 @@ class RetryInterceptor extends Interceptor {
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
if (err.response != null) return handler.next(err);
if (err.response != null) {
final options = err.requestOptions;
if (options.followRedirects && options.maxRedirects > 0) {
final status = err.response!.statusCode;
if (status != null && 300 <= status && status < 400) {
var redirectUrl = err.response!.headers.value('location');
if (redirectUrl != null) {
var uri = Uri.parse(redirectUrl);
if (!uri.hasScheme) {
uri = options.uri.resolveUri(uri);
redirectUrl = uri.toString();
}
(options..path = redirectUrl).maxRedirects--;
if (status == 303) {
options.data = null;
options.method = 'GET';
}
Request.dio
.fetch(options)
.then((i) => handler.resolve(i
..redirects.add(RedirectRecord(status, options.method, uri))
..isRedirect = true))
.onError<DioException>((error, _) => handler.next(error));
return;
}
}
}
return handler.next(err);
} else {
switch (err.type) {
case DioExceptionType.connectionError:
case DioExceptionType.connectionTimeout:
@@ -23,7 +51,8 @@ class RetryInterceptor extends Interceptor {
() => Request.dio
.fetch(err.requestOptions)
.then(handler.resolve)
.onError<DioException>((error, _) => handler.reject(error)));
.onError<DioException>(
(error, _) => handler.reject(error)));
} else {
handler.next(err);
}
@@ -32,4 +61,5 @@ class RetryInterceptor extends Interceptor {
return handler.next(err);
}
}
}
}

View File

@@ -535,8 +535,8 @@ class _PostPanelState extends CommonCollapseSlidePageState<PostPanel> {
];
}
void _onPost({String? url}) async {
Request().post(
Future _onPost({String? url}) {
return Request().post(
url ?? '${GStorage.blockServer}/api/skipSegments',
data: {
'videoID': videoDetailController.bvid,
@@ -557,14 +557,9 @@ class _PostPanelState extends CommonCollapseSlidePageState<PostPanel> {
)
.toList(),
},
options: Options(
validateStatus: (int? status) {
return status != null &&
((status >= 200 && status < 300) ||
const [400, 403, 429, 409].contains(status));
},
),
).then(
options: Options(followRedirects: true),
)
.then(
(res) {
if (res.statusCode == 200) {
Get.back();
@@ -578,12 +573,13 @@ class _PostPanelState extends CommonCollapseSlidePageState<PostPanel> {
}
} else {
SmartDialog.showToast(
'提交失败: ${const {
400: '参数错误',
403: '被自动审核机制拒绝',
429: '重复提交太快',
409: '重复提交'
}[res.statusCode] ?? res.statusCode}',
'提交失败: ${switch (res.statusCode) {
400 => '参数错误',
403 => '被自动审核机制拒绝',
429 => '重复提交太快',
409 => '重复提交',
_ => res.statusCode.toString()
}}',
);
}
},

View File

@@ -129,20 +129,13 @@ class AccountManager extends Interceptor {
account.cookieJar.loadForRequest(options.uri).then((cookies) {
final previousCookies =
options.headers[HttpHeaders.cookieHeader] as String?;
String newCookies = getCookies([
final newCookies = getCookies([
...?previousCookies
?.split(';')
.where((e) => e.isNotEmpty)
.map((c) => Cookie.fromSetCookieValue(c)),
...cookies,
]);
if (options.extra['cookie'] != null) {
if (newCookies.isEmpty) {
newCookies = '${options.extra['cookie']}';
} else {
newCookies += ';${options.extra['cookie']}';
}
}
options.headers[HttpHeaders.cookieHeader] =
newCookies.isNotEmpty ? newCookies : '';
handler.next(options);
@@ -177,7 +170,7 @@ class AccountManager extends Interceptor {
}
@override
void onError(DioException err, ErrorInterceptorHandler handler) async {
void onError(DioException err, ErrorInterceptorHandler handler) {
if (err.requestOptions.method != 'POST') {
toast(err);
}
@@ -198,7 +191,7 @@ class AccountManager extends Interceptor {
}
}
static void toast(err) {
static void toast(DioException err) {
const List<String> skipShow = [
'heartbeat',
'history/report',