gpt4 book ai didi

http - ruSTLs HTTP 请求响应 301

转载 作者:行者123 更新时间:2023-12-03 11:47:52 25 4
gpt4 key购买 nike

使用 example rustls client code 发送 HTTP 请求

let mut config = rustls::ClientConfig::new();
config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);

let dns_name = webpki::DNSNameRef::try_from_ascii_str("google.com").unwrap();
let mut sess = rustls::ClientSession::new(&Arc::new(config), dns_name);
let mut sock = TcpStream::connect("google.com:443").unwrap();
let mut tls = rustls::Stream::new(&mut sess, &mut sock);
tls.write(concat!("GET / HTTP/1.1\r\n",
"Host: google.com\r\n",
"Connection: close\r\n",
"Accept-Encoding: identity\r\n",
"\r\n")
.as_bytes())
.unwrap();
let ciphersuite = tls.sess.get_negotiated_ciphersuite().unwrap();
writeln!(&mut std::io::stderr(), "Current ciphersuite: {:?}", ciphersuite.suite).unwrap();
let mut plaintext = Vec::new();
tls.read_to_end(&mut plaintext).unwrap();
stdout().write_all(&plaintext).unwrap();
导致重定向消息:
Current ciphersuite: TLS13_CHACHA20_POLY1305_SHA256
HTTP/1.1 301 Moved Permanently
Location: https://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sat, 05 Sep 2020 13:56:25 GMT
Expires: Mon, 05 Oct 2020 13:56:25 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 220
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Alt-Svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Connection: close

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>
这是此示例代码的正确行为吗?我应该改变什么才能收到 OK 响应?

最佳答案

Is this the correct behavior for this example code?


是的。这也可以在使用其他工具尝试相同类型的请求时进行检查:
$ curl -v https://google.com
...
< HTTP/1.1 301 Moved Permanently
< Location: https://www.google.com/

What am I supposed to change to receive an OK response?


只需使用重定向中给出的 URL,即 https://www.google.com而不是 https://google.com .
$ curl -v https://www.google.com
...
< HTTP/1.1 200 OK

关于http - ruSTLs HTTP 请求响应 301,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63754758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com