所以我有以下代码将多线程请求发送到域列表。到目前为止,我能够从响应中获取单个数据,例如字节、url 或状态码等,但不能将它们全部放在一起。我想将所有这些值存储到向量中,以便可以将其写入文件。我知道这可能是一个超级愚蠢的问题,但我已经研究了好几天,无法弄清楚。任何帮助表示赞赏!
#[tokio::main]
async fn get_request(urls: Vec<&str>, paths: Vec<&str>) {
let client = Client::new();
for path in paths {
let urls = urls.clone();
let bodies = stream::iter(urls)
.map(|url| {
let client = &client;
async move {
let mut full_url = String::new();
full_url.push_str(url);
full_url.push_str(path);
let resp = client
.get(&full_url)
.timeout(Duration::from_secs(3))
.send()
.await;
resp
}
})
.buffer_unordered(PARALLEL_REQUESTS);
bodies
.for_each(|b| async {
match b {
Ok(b) => {
let mut body_test = &b.bytes().await;
match body_test {
Ok(body_test) => {
let mut stringed_bytes = str::from_utf8(&body_test);
match stringed_bytes {
Ok(stringed_bytes) => {
println!("stringed bytes: {}", stringed_bytes);
}
Err(e) => println!("Stringified Error: {}", e),
}
}
Err(e) => println!("body Error: {}", e),
}
}
Err(e) => println!("Got an error: {}", e),
}
})
.await;
}
}