Telnet 到 google.com - 您的客户端发出了格式错误或非法的请求

Telnet 到 google.com - 您的客户端发出了格式错误或非法的请求

我了解 HTTP 协议。我想使用 telnet 获取 google 查询的结果。

我这样做了:

telnet google.com 80
Trying 172.217.20.206...
Connected to google.com.
Escape character is '^]'.
GET /search?q=hello+world http/1.1
host: google.com

但得到的答复并不是我想要的:

HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Content-Length: 1555
Date: Thu, 13 Apr 2017 21:48:39 GMT

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>400.</b> <ins>That’s an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>
Connection closed by foreign host.

我的请求有何不好?

答案1

我的请求有何不好?

你不需要http/1.1

$ telnet google.com 80
Trying 216.58.198.174...
Connected to google.com.
Escape character is '^]'.
GET /search?q=hello+world

HTTP/1.0 302 Found
Location: http://www.google.co.uk/search?q=hello+world&gws_rd=cr&ei=rQbwWO-EFuPGgAbQsqvoCQ
Cache-Control: private
Content-Type: text/html; charset=UTF-8
P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
Date: Thu, 13 Apr 2017 23:15:57 GMT
Server: gws
Content-Length: 285
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: NID=101=OFb36GmHOF7h2VCg_9pDdxY1vDrp5JYXjQOEIpjyIR1LJt228TZHgfC_pfNeFAQsBvR1REMMBXDkJoyuI45KYt1u3OrSvWQu7guevtsYF2RGJiTUqx9gZLcxbfXcNdn6; expires=Fri, 13-Oct-2017 23:15:57 GMT; path=/; domain=.google.com; HttpOnly

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.uk/search?q=hello+world&amp;gws_rd=cr&amp;ei=rQbwWO-EFuPGgAbQsqvoCQ">here</A>.
</BODY></HTML>
Connection closed by foreign host.
$

答案2

您的请求中的http/1.1必须是大写,即HTTP/1.1

看到这个堆栈溢出邮政。

相关内容