简体   繁体   中英

HTTP POST in Flutter getting Cookie

I want to make a HTTP POST with Flutter. I want to log into a website and get the Cookie. With the shown HTTP Post I cannot log in. I don´t get a Cookie. I tried to make the same HTTP POST with the Software Postman and it works. Does anybody know, where the difference between the HTTP POSTs is?

HTTP POST with Postman:

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

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);

I suggest you to use dio with its plugin dio_cookie_manager .

Add dependencies to your project:

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

And then use it in your code:

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

Use PersistCookieJar() instead of CookieJar to save cookies in a file (and not in RAM). Check the documentations for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM