【问题标题】:HTTP POST in Flutter getting CookieFlutter 中的 HTTP POST 获取 Cookie
【发布时间】:2026-02-03 11:35:01
【问题描述】:

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

使用 Postman 的 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--  

颤振代码:

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】:

    我建议你使用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 中)。 详情请查看文档。

    【讨论】:

    • 我用 dio 测试过,但它不起作用。在响应的标题中不是“Set-Cookie”。响应的状态代码应该是 303,但我得到 200。我可以启用重定向还是自动工作?