Flutter http streamedResponse.stream 和 streamedResponse.sink
Flutter http streamedResponse.stream and streamedResponse.sink
我正在学习Flutter http包的第三个例子,这是代码的基础:https://pub.dev/packages/http
When the request is sent via BaseClient.send, only the headers and whatever data has already been written to StreamedRequest.stream will be sent immediately. More data will be sent as soon as it's written to StreamedRequest.sink, and when the sink is closed the request will end.
https://pub.dev/documentation/http/latest/http/StreamedRequest-class.html
从文档中,我不明白我们应该如何写入 StreamedRequest.stream
? (立即发送数据)
StreamedResponse.sink 基本上不是我们添加 HTTP POST 的请求主体的地方:为什么它只接受 List<int>
?不应该是Map<String, String>
吗?如果不是那么我们应该在哪里添加请求正文? NEW:
即使我使用 ut8.encode 对其进行编码,当我调试时它仍然没有出现在 Fiddler 的 WebForms 上,如何正确发送 x-www-form-urlencoded?:
代码:
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"}'));
- 为什么我必须关闭接收器才能访问 StreamedResponse 的属性?
streamedRequest.sink.close();
更新:
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();
}
}
所以我 运行 前两个函数,但是 _body
变量直到我 运行 _httpClose()
函数才出现在我的屏幕上。
是
Map<String,String>
无法直播。
Streamed 意味着每次流发出数据块并且服务器(或浏览器发送缓冲区)准备好接收更多数据时,数据就会分块发送。
List<int>
可以分块,因为一次发送多少字节并不重要。
如果您拥有现成可用的所有数据,您可能不想使用 StreamedRequest,特别是如果它不是数据块。 https://en.wikipedia.org/wiki/Binary_large_object
utf8.encode
可用于对流发出的块进行编码,但它本身不提供流,因此您无法将 utf8.encode
的结果添加到接收器。
- 我不明白那个问题。您想要访问哪些属性以及您在尝试时 运行 会遇到什么问题?
对我来说,您的用例似乎不需要使用 StreamedRequest。
我正在学习Flutter http包的第三个例子,这是代码的基础:https://pub.dev/packages/http
When the request is sent via BaseClient.send, only the headers and whatever data has already been written to StreamedRequest.stream will be sent immediately. More data will be sent as soon as it's written to StreamedRequest.sink, and when the sink is closed the request will end.
https://pub.dev/documentation/http/latest/http/StreamedRequest-class.html
从文档中,我不明白我们应该如何写入
StreamedRequest.stream
? (立即发送数据)StreamedResponse.sink 基本上不是我们添加 HTTP POST 的请求主体的地方:为什么它只接受
List<int>
?不应该是Map<String, String>
吗?如果不是那么我们应该在哪里添加请求正文?NEW:
即使我使用 ut8.encode 对其进行编码,当我调试时它仍然没有出现在 Fiddler 的 WebForms 上,如何正确发送 x-www-form-urlencoded?:
代码:
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"}'));
- 为什么我必须关闭接收器才能访问 StreamedResponse 的属性?
streamedRequest.sink.close();
更新:
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();
}
}
所以我 运行 前两个函数,但是 _body
变量直到我 运行 _httpClose()
函数才出现在我的屏幕上。
是
Map<String,String>
无法直播。 Streamed 意味着每次流发出数据块并且服务器(或浏览器发送缓冲区)准备好接收更多数据时,数据就会分块发送。List<int>
可以分块,因为一次发送多少字节并不重要。 如果您拥有现成可用的所有数据,您可能不想使用 StreamedRequest,特别是如果它不是数据块。 https://en.wikipedia.org/wiki/Binary_large_object
utf8.encode
可用于对流发出的块进行编码,但它本身不提供流,因此您无法将 utf8.encode
的结果添加到接收器。
- 我不明白那个问题。您想要访问哪些属性以及您在尝试时 运行 会遇到什么问题?
对我来说,您的用例似乎不需要使用 StreamedRequest。