Dart - 使用 cookie 请求 GET [英] Dart - Request GET with cookie
本文介绍了Dart - 使用 cookie 请求 GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试发出一个获取请求,但我需要放置 cookie.
I'm trying to make a get request but I need to put the cookie.
使用 curl 工作:
Using the curl works:
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
可以更轻松地使用包含 cookie 的请求:
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屋!
查看全文