这是我的代码:
Future<String> fetch(url) async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
var cj = new PersistCookieJar(tempPath);
HttpClient client = new HttpClient();
var request = await client.getUrl(Uri.parse(url));
request.cookies.addAll(cj.loadForRequest(Uri.parse(url)));
var response = await request.close();
cj.saveFromResponse(Uri.parse(url), response.cookies);
// I tried using .toString() but it returns an " Instance of '_HttpClientResponse' "
return response.toString();
}
我花了几个小时试图找到一种方法来返回响应正文,但没有成功,有人可以帮助我。
答案 0 :(得分:1)
没关系,我已修复它。
我使用了以下代码:
Future<String> fetch(url) async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
var cj = new PersistCookieJar(tempPath);
HttpClient client = new HttpClient();
var request = await client.getUrl(Uri.parse(url));
request.cookies.addAll(cj.loadForRequest(Uri.parse(url)));
var response = await request.close();
cj.saveFromResponse(Uri.parse(url), response.cookies);
// I had to transform the response for it to work
var body = await response.transform(Utf8Decoder(allowMalformed: true)).join();
return body;
}