飞镖/颤振登录发布请求

时间:2020-02-29 02:43:52

标签: flutter dart

我正在尝试登录该论坛,我尝试了几种方法(Dio,Requests),但没有结果。

我用邮递员使用其他语言和库(例如curl,python,node)对其进行了测试,它的工作原理很吸引人。

[ EDIT ]

好,终于知道了!

登录后,服务器将返回状态码303。 并且由于followredirects默认情况下设置为true,因此发出了没有会话cookie的新请求。

这就是为什么我从未在响应标头中找到会话cookie的原因。

类似于lib python请求中的“历史”的方法在这里会很好。

新:

import 'package:http/http.dart';

void main() async {

final request = new Request(
    'POST', Uri.parse('https://xenforo.com/community/login/login'))

  ..headers.addAll({"Content-Type": "application/x-www-form-urlencoded"})

  ..bodyFields = {'login':'myuser',
                'password': 'mypass'}

  ..followRedirects = false;  // don't follow the damn 303 code if you're not 
                              // going to set the cookies automatically.

   final response = await request.send();
   print(response.statusCode);  // 303 redirected successfully logged in!
   print(response.headers);  // session token: xf_session=oisufhisuefhsef...

}

import 'dart:io';

void main() async {

  final client = HttpClient();
  final request = await 
  client.postUrl(Uri.parse("https://xenforo.com/community/login/login"));
  request.headers.set(HttpHeaders.contentTypeHeader, "application/x-www_form-urlencoded");
  request.followRedirects = false;
  request.write("login=myusername&password=mypass");
 final response = await request.close();

 print(response.statusCode);
 print(response.headers);

}

2 个答案:

答案 0 :(得分:0)

您应该使用awaitthen,请尝试以下代码:

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

void main() {
  http.get("https://xenforo.com/community/").then((response) {
    var ls = response.headers['set-cookie'].split(';');
    final cookie = ls[0] + '; ' + ls[5].split(',')[1];
    login(cookie);
  });
}

void login(String cookie) {
  http.post("https://xenforo.com/community/login/login/", headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Cookie': cookie
  }, body: {
    'login': 'myusernam',
    'password': 'mypass'
  }).then((response) {
    print(response.statusCode);
    print(response.body);
  });
}

答案 1 :(得分:0)

好,终于得到了它。每次登录成功,请求都会自动重定向,而无需会话cookie。 FollowRedirects需要设置为false才能正常工作,并且http.post没有此选项。

一种类似于lib python请求的“历史记录”的方法。