Created
February 10, 2020 18:55
-
-
Save odra/8829a082e828454639f5f6a302aac634 to your computer and use it in GitHub Desktop.
rust http client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::prelude::*; | |
use std::net::TcpStream; | |
fn main() { | |
let mut b = vec![0; 4096]; | |
let mut s = String::new(); | |
let headers = "GET /ip HTTP/1.1\r\nHost: httpbin.org\r\nUser-Agent: foobar/0.0.1\r\nAccept: */*\r\n\r\n"; | |
let mut stream = TcpStream::connect("httpbin.org:80").unwrap(); | |
stream.write(headers.as_bytes()).unwrap(); | |
loop { | |
match stream.read(&mut b) { | |
Ok(n) => { | |
if n == 0 { | |
break; | |
} | |
let text = std::str::from_utf8(&b).unwrap(); | |
s.push_str(text); | |
}, | |
Err(e) => panic!(e) | |
} | |
} | |
print!("{}", s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment