Flutter中的HTTP POST获取Cookie

问题描述 投票:0回答:1

我想用Flutter进行HTTP POST。我想登录一个网站并获取Cookie。使用显示的HTTP帖子,我无法登录。我没有得到Cookie。我尝试使用Software Postman进行相同的HTTP POST,并且可以正常工作。有人知道HTTP POST之间的区别在哪里吗?

与邮递员的HTTP POST:

POST /fuer-studierende/intranet/ HTTP/1.1
Host: www.phwt.de
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
User-Agent: PostmanRuntime/7.19.0
Accept: */*
Cache-Control: no-cache
Postman-Token: c1daecce-cc06-432a-ac5d-888d90b345c2,7eb5d96f-e42c-4dee-932e-c4c30d12d499
Host: www.phwt.de
Accept-Encoding: gzip, deflate
Content-Length: 734
Connection: keep-alive
cache-control: no-cache


Content-Disposition: form-data; name="user"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--,
Content-Disposition: form-data; name="user"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="pass"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--,
Content-Disposition: form-data; name="user"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="pass"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="logintype"

login
------WebKitFormBoundary7MA4YWxkTrZu0gW--,
Content-Disposition: form-data; name="user"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="pass"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="logintype"

login
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="pid"

128
------WebKitFormBoundary7MA4YWxkTrZu0gW--,
Content-Disposition: form-data; name="user"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="pass"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="logintype"

login
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="pid"

128
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="submit"

Anmelden
------WebKitFormBoundary7MA4YWxkTrZu0gW--,
Content-Disposition: form-data; name="user"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="pass"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="logintype"

login
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="pid"

128
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="submit"

Anmelden
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="tx_felogin_pi1[noredirect]"

0
------WebKitFormBoundary7MA4YWxkTrZu0gW--  

Flutter代码:

Map<String, String> form3 = {
    "user":"test",
    "pass":"test",
    "logintype":"login",
    "pid":"128",
    "submit":"Anmelden",
    "tx_felogin_pi1[noredirect]":"0"
  };

  Map<String, String> h1234 = {
    "Content-Type": "application/x-www-form-urlencoded",
    "User-Agent":"PostmanRuntime/7.19.0",
    "Accept": "*/*",
    "Cache-Control":"no-cache",
    "Postman-Token":"c1daecce-cc06-432a-ac5d-888d90b345c2,7eb5d96f-e42c-4dee-932e-c4c30d12d499",
    "Host":"www.phwt.de",
    "Accept-Encoding":"gzip, deflate",
    "Content-Length":"122",
    "Connection":"keep-alive"};

http.Response res = await ioClient.post("https://www.phwt.de/fuer-studierende/intranet/",body:form3, headers: h1234);
http cookies flutter http-post postman
1个回答
0
投票

我建议您将dio与它的插件dio_cookie_manager一起使用。

向项目添加依赖项:

dependencies:
    dio: 3.x #latest version
    dio_cookie_manager: 1.0.x  #latest version

然后在您的代码中使用它:

import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:cookie_jar/cookie_jar.dart';
void login() async {
  try {
    var dio =  Dio();
    var cookieJar=CookieJar();
    dio.interceptors.add(CookieManager(cookieJar));
    await dio.post("your URL", data: [your data object]);

    // Print cookies
    print(cookieJar.loadForRequest(Uri.parse("your URL")));
  } catch (e) {
    print(e);
  }
}

使用PersistCookieJar()而不是CookieJar将cookie保存在文件中(而不是RAM中)。查看文档以获取详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.