我的Linux机器上安装了Apache,我正在尝试编写一个HTTP客户端来维护TCP连接以检索网页。
要做到这一点,我首先打开一个socket到localhost并编写以下头
GET / HTTP/1.0
Host: http://localhost:80
User-Agent: My-User-Agent 1.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Cache-Control: no-cache
Accept-Language: en;q=0.7,en-us;q=0.3
Connection: close
但是,apache给了我一个Bad Request错误,看起来像这样
HTTP/1.1 400 Bad Request
Date: Mon, 21 Nov 2011 23:58:03 GMT
Server: Apache/2.2.20 (Ubuntu)
Vary: Accept-Encoding
Content-Length: 311
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title> </head><body> <h1>Bad Request</h1>
<p>Your browser sent a request that this server could not
understand.<br /> </p> <hr> <address>Apache/2.2.20 (Ubuntu) Server at
http://localhost:80 Port 80</address> </body></html>
apache错误日志说明了这一点: [Mon Nov 21 15:58:03 2011] [错误] [客户端127.0.0.1]客户端发送格式错误的主机标题
有人可以帮我解决这个问题。标题有什么问题?
答案 0 :(得分:3)
在这种情况下,主机应该是简单的 localhost :
GET / HTTP/1.0
Host: localhost
...