为什么带有Vanilla Rust的HTTP GET请求无法得到答案? [英] Why does a HTTP GET request with vanilla Rust get no answer?
本文介绍了为什么带有Vanilla Rust的HTTP GET请求无法得到答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,但是当我启动应用程序时,它会请求页面但没有任何答案.
I have the following code but when I start the app it requests the page but gets no answer.
use std::io::Read;
use std::io::Result;
use std::io::Write;
use std::net::TcpStream;
fn main() {
if let Err(err) = connect() {
println!("err = {}", err);
}
}
fn connect() -> Result<()> {
let mut stream = TcpStream::connect("www.google.de:80")?;
let mut request_data = String::new();
request_data.push_str("GET / HTTP/1.1");
request_data.push_str("\r\n");
request_data.push_str("Host: www.google.de");
request_data.push_str("\r\n");
request_data.push_str("\r\n");
println!("request_data = {:?}", request_data);
let request = stream.write_all(request_data.as_bytes())?;
println!("request = {:?}", request);
let mut buf = String::new();
let result = stream.read_to_string(&mut buf)?;
println!("result = {}", result);
println!("buf = {}", buf);
Ok(())
}
推荐答案
您应该传递Connection: close
HTTP标头,以便服务器可以正确关闭连接.
You should pass the Connection: close
HTTP header so the server can properly close the connection.
请注意,Google将返回压缩的内容,因此您将收到错误消息: err =流不包含有效的UTF-8 ,甚至发送了标头Accept-Encoding: identity
.
Note that Google will returns compressed content, so you'll get an error: err = stream did not contain valid UTF-8, even sending the header Accept-Encoding: identity
.
将主机的测试更改为 www.rust-lang.org :
use std::io::Read;
use std::io::Result;
use std::io::Write;
use std::net::TcpStream;
fn main() {
if let Err(err) = connect() {
println!("err = {}", err);
}
}
fn connect() -> Result<()> {
let mut stream = TcpStream::connect("www.rust-lang.org:80")?;
let mut request_data = String::new();
request_data.push_str("GET / HTTP/1.0");
request_data.push_str("\r\n");
request_data.push_str("Host: www.rust-lang.org");
request_data.push_str("\r\n");
request_data.push_str("Connection: close"); // <== Here!
request_data.push_str("\r\n");
request_data.push_str("\r\n");
println!("request_data = {:?}", request_data);
let request = stream.write_all(request_data.as_bytes())?;
println!("request = {:?}", request);
let mut buf = String::new();
let result = stream.read_to_string(&mut buf)?;
println!("result = {}", result);
println!("buf = {}", buf);
Ok(())
}
将导致:
request_data = "GET / HTTP/1.0\r\nHost: www.rust-lang.org\r\nConnection: close\r\n\r\n"
request = ()
result = 554
buf = HTTP/1.1 301 Moved Permanently
Server: CloudFront
Date: Sun, 13 May 2018 04:46:00 GMT
Content-Type: text/html
Content-Length: 183
Connection: close
Location: https://www.rust-lang.org/
X-Cache: Redirect from cloudfront
Via: 1.1 17af5476e6187c3063f9ba7f797d342b.cloudfront.net (CloudFront)
X-Amz-Cf-Id: lzMymVQ6vTv27VxSU4J0MR6EmgPDCDqO8KFimo1kPvxmQxcKjT33jg==
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>CloudFront</center>
</body>
</html>
这篇关于为什么带有Vanilla Rust的HTTP GET请求无法得到答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文