重定向循环检测到Dart

时间:2018-08-18 21:18:06

标签: json rest dart

我在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);
  }
}

它可以正常工作,但是当我将查询网址更改为 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)

为什么会这样?当我使用浏览器时就可以使用

1 个答案:

答案 0 :(得分:4)

第一个请求以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标头,然后将内容用作下一个请求的cookie,而不是对其进行硬编码。