Rust で ファイルダウンロード

そんなに難しいわけではないです。

[dependencies]
hyper = "^0.7"
// hyper
extern crate hyper;
use std::io::Read;
use hyper::Client;
use hyper::client::{Request, Response};
use hyper::header::{Connection, Headers, UserAgent};
fn main () { let mut client = Client::new(); file_download(&client, "http://");
}
fn file_download<'a>(client: &'a Client, url: &str) { let mut oRes = client.get(url) .header(Connection::close()) .header(UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) \ AppleWebKit/537.36 (KHTML, like Gecko) \ Chrome/48.0.2564.97 Safari/537.36" .to_string())) .send(); let mut res = oRes.unwrap(); println!("{:?}", res); let mut body: Vec<u8> = vec![]; res.read_to_end(&mut body).unwrap(); let mut f = File::create("foo.jpg").unwrap(); println!("{:?}", f); f.write_all(&body);
}
カテゴリー: Rust