Flutter 颤振http streamdresponse.stream和streamdresponse.sink
我正在学习颤振http包的第三个示例,这是代码的基础: 当通过BaseClient.send发送请求时,仅会立即发送头和任何已写入StreamedRequest.stream的数据。写入StreamedRequest.sink后,将立即发送更多数据,当sink关闭时,请求将结束Flutter 颤振http streamdresponse.stream和streamdresponse.sink,flutter,http,flutter-http,Flutter,Http,Flutter Http,我正在学习颤振http包的第三个示例,这是代码的基础: 当通过BaseClient.send发送请求时,仅会立即发送头和任何已写入StreamedRequest.stream的数据。写入StreamedRequest.sink后,将立即发送更多数据,当sink关闭时,请求将结束 从文档中,我不明白我们应该如何编写streamdRequest.stream?(立即发送数据) streamdresponse.sink不是我们添加HTTP POST请求主体的地方吗:为什么它只接受列表?它不应该是一
streamdRequest.stream
?(立即发送数据)
列表
?它不应该是一张地图吗?如果不是,那么我们应该在哪里添加请求主体<代码>新建:即使我使用ut8.encode对其进行编码,在调试时它仍然不会显示在Fiddler的WebForms上,如何正确发送x-www-form-urlencoded
\u httpClose()
函数之前,\u body
变量不会显示在屏幕上
Map
无法流式传输。
Streamed是指每次流发出一个数据块,并且服务器(或浏览器发送缓冲区)准备接收更多数据时,数据以块的形式发送。
列表
可以分块,因为一次发送多少字节无关紧要。
如果您有所有可用的数据,您可能不想使用StreamedRequest,特别是如果它不是一个数据块的话
utf8.encode
可用于对流发出的块进行编码,但它本身不提供流,因此无法将utf8.encode
的结果添加到接收器
在我看来,您不需要在您的用例中使用StreamedRequest。非常感谢,这一切现在都是有意义的,对于3,基本上当我想检查服务器的响应时,比如主体,它在我关闭水槽之前不会显示,我会在一分钟内更新我的代码。好的,这是因为在流关闭之前无法完成请求(表示不再有任何数据),并且您显然无法在请求之前访问响应;-)您所做的更新代码是完全可用的版本?@Cassiosefrin我是一名颤振学习者,我试图完全学习http颤振包,因此我制作了一个应用程序来测试它的所有功能。由于您感兴趣,我在这里为您上传了完整的工作版本:所有代码都在一个dart文件中,复制粘贴到您的颤振测试项目中,然后构建应用程序并测试所有内容!沙岩。现在我只需要检查这些流式请求是否有效。我以不同的方式完成了它:final request=request('GET',Uri.parse(url));request.headers.addAll(headers);最终流响应=等待客户端()。发送(请求);此外,我需要配置超时,但Client().send没有执行此操作所需的参数。
userAgentClient = UserAgentClient(userAgent, client);
streamedRequest = http.StreamedRequest('POST', Uri(scheme: 'http', path: '/posts/', host: 'jsonplaceholder.typicode.com'));
streamedRequest.sink.add([123, 456]); // It has to be a List<int>
//NEW:
streamedRequest.sink.add(utf8.encode('username=123&password=456'));
streamedRequest.sink.add(utf8.encode('{"username":"123","password":"456"}'));
class UserAgentClient extends http.BaseClient {
final String userAgent;
final http.Client client;
UserAgentClient(this.userAgent, this.client);
Future<http.StreamedResponse> send(http.BaseRequest request){
request.headers['user-agent'] = userAgent;
return client.send(request);
}
}
dynamic _status = '';
dynamic _body = '';
dynamic _headers = '';
String _reason = '';
http.Client client = http.Client();
String userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36';
UserAgentClient userAgentClient;
http.StreamedRequest streamedRequest;
void _httpStreamed(){
userAgentClient = UserAgentClient(userAgent, client);
streamedRequest = http.StreamedRequest('POST', Uri(scheme: 'http', path: '/posts/', host: 'jsonplaceholder.typicode.com'));
streamedRequest.sink.add(utf8.encode('{"username":"123","password":"456"}'));
setState(() {
_status = streamedRequest.url;
_body = '';
_headers = '';
_reason = '';
});
}
void _httpSend() async{
http.StreamedResponse streamedResponse;
streamedResponse = await userAgentClient.send(streamedRequest);
streamedResponse.stream.listen(
(value) async{
_body = http.ByteStream.fromBytes(value);
_body = await _body.bytesToString();
},
onError: (e, sT) {
SnackBar sBar = SnackBar(content: Text('$e\n$sT',));
Scaffold.of(context).showSnackBar(sBar);
},
onDone: () {
SnackBar sBar = SnackBar(content: Text('Done lol'),);
Scaffold.of(context).showSnackBar(sBar);
},
);
setState(() {
_body;
_status = streamedResponse.statusCode;
_headers = streamedResponse.headers;
_reason = streamedResponse.reasonPhrase;
});
}
}
void _httpClose(){
if (streamedRequest != null){
streamedRequest.sink.close();
}
}