gpt4 book ai didi

json - 重定向循环检测到 Dart

转载 作者:行者123 更新时间:2023-12-05 01:04:58 26 4
gpt4 key购买 nike

我在 Dart 中有以下代码

import 'dart:io';
import 'package:http/http.dart' as http;

main() async {
final url = 'https://jsonplaceholder.typicode.com/users';
final response = await http.get(url);

if (response.statusCode == HttpStatus.ok) {
print(response.body);
}
}

它工作正常,但是当我将查询 url 更改为
http://loterias.caixa.gov.br/wps/portal/loterias/landing/lotofacil/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOLNDH0MPAzcDbz8vTxNDRy9_Y2NQ13CDA0sTIEKIoEKnN0dPUzMfQwMDEwsjAw8XZw8XMwtfQ0MPM2I02-AAzgaENIfrh-FqsQ9wBmoxN_FydLAGAgNTKEK8DkRrACPGwpyQyMMMj0VAcySpRM!/dl5/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_61L0H0G0J0VSC0AC4GLFAD2003/res/id=buscaResultado/c=cacheLevelPage/=/?timestampAjax=1534624870817&concurso=1703
import 'dart:io';
import 'package:http/http.dart' as http;

main() async {
final url =
'http://loterias.caixa.gov.br/wps/portal/loterias/landing/lotofacil/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOLNDH0MPAzcDbz8vTxNDRy9_Y2NQ13CDA0sTIEKIoEKnN0dPUzMfQwMDEwsjAw8XZw8XMwtfQ0MPM2I02-AAzgaENIfrh-FqsQ9wBmoxN_FydLAGAgNTKEK8DkRrACPGwpyQyMMMj0VAcySpRM!/dl5/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_61L0H0G0J0VSC0AC4GLFAD2003/res/id=buscaResultado/c=cacheLevelPage/=/?timestampAjax=1534624870817&concurso=1703';
final response = await http.get(url);

if (response.statusCode == HttpStatus.ok) {
print(response.body);
}
}

出现以下错误:
    Unhandled exception:
Redirect loop detected
#0 IOClient.send (package:http/src/io_client.dart:64:7)
<asynchronous suspension>
#1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:171:38)
<asynchronous suspension>
#2 BaseClient.get (package:http/src/base_client.dart:34:5)
#3 get.<anonymous closure> (package:http/http.dart:47:34)
#4 _withClient (package:http/http.dart:167:20)
<asynchronous suspension>
#5 get (package:http/http.dart:47:3)

为什么会出现这种情况?当我使用浏览器时它可以工作

最佳答案

第一个请求响应一个 cookie 和 HttpStatus.movedTemporarily , 重定向到相同的 URL。
浏览器在跟踪重定向时在其第二个请求中发送 cookie。
第二个请求以 HttpStatus.ok 结束因为它包含预期的 cookie。

import 'dart:io';
import 'package:http/http.dart' as http;

void main() async {
var url =
'http://loterias.caixa.gov.br/wps/portal/loterias/landing/lotofacil/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOLNDH0MPAzcDbz8vTxNDRy9_Y2NQ13CDA0sTIEKIoEKnN0dPUzMfQwMDEwsjAw8XZw8XMwtfQ0MPM2I02-AAzgaENIfrh-FqsQ9wBmoxN_FydLAGAgNTKEK8DkRrACPGwpyQyMMMj0VAcySpRM!/dl5/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_61L0H0G0J0VSC0AC4GLFAD2003/res/id=buscaResultado/c=cacheLevelPage/=/?timestampAjax=1534624870817&concurso=1703';
var isRedirect = true;

while (isRedirect) {
final client = http.Client();
final request = http.Request('GET', Uri.parse(url))
..followRedirects = false
..headers['cookie'] = 'security=true';
print(request.headers);
final response = await client.send(request);

if (response.statusCode == HttpStatus.movedTemporarily) {
isRedirect = response.isRedirect;
url = response.headers['location'];
// final receivedCookies = response.headers['set-cookie'];
} else if (response.statusCode == HttpStatus.ok) {
print(await response.stream.join(''));
}
}
}

考虑阅读 set-cookie来自第一个响应的 header ,并将内容用作下一个请求的 cookie,而不是对其进行硬编码。

关于json - 重定向循环检测到 Dart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51912621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com