Dart-使用Cookie请求GET [英] Dart - Request GET with cookie

查看:139
本文介绍了Dart-使用Cookie请求GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取请求,但需要放入cookie。

I'm trying to make a get request but I need to put the cookie.

使用curl函数:

curl -v --cookie sessionid = asdasdasqqwd< my_site>

下面的函数不带任何东西

But the function below does not bring anything

import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' as parser;
import 'package:html/dom.dart';

...
parseHtml() async {
   http.Response response = await http.get (
     <my_site>,
     headers: {"sessionid": "asdasdasqqwd"}
   );
   Document document = parser.parse (response.body);
   print(document.text);
}

是否可以将Cookie放在Dart的get请求中?

Would there be any way to put the cookie on the get request in Dart?

推荐答案

您可以使用 http.get(URL,Headers Map)函数并在标题映射中手动创建cookie,但是使用 HttpClient

You could use the http.get(Url, Headers Map) function and manually create your cookies in the header map, but it is easier to make a request with cookies included by using HttpClient:

import 'dart:convert';
import 'dart:io';

import 'package:html/dom.dart';
import 'package:html/parser.dart' as parser;

parseHtml() async {
  HttpClient client = new HttpClient();
  HttpClientRequest clientRequest =
      await client.getUrl(Uri.parse("http: //www.example.com/"));
  clientRequest.cookies.add(Cookie("sessionid", "asdasdasqqwd"));
  HttpClientResponse clientResponse = await clientRequest.close();
  clientResponse.transform(utf8.decoder).listen((body) {
    Document document = parser.parse(body);
    print(document.text);
  });
}

这篇关于Dart-使用Cookie请求GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆