随笔 - 9  文章 - 21  trackbacks - 0
<2008年2月>
272829303112
3456789
10111213141516
17181920212223
2425262728291
2345678

常用链接

留言簿(1)

随笔分类(9)

随笔档案(9)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

HTTP

HTTP就是一种请求/响应模式。

HTTP事务

  1. 连接 建立和服务器的连接
  2. 查询 请求服务器上的资源
  3. 处理 服务器接受和处理请求
  4. 响应 服务器将请求的资源发送回客户端
  5. 终止 事务完成后,关闭连接,除非客户机与服务器之间立即发生另一事务。

用telnet程序验证这个过程

连接

telnet www.baidu.com 80

80是HTTP协议默认的端口。

查询

GET / HTTP/1.0↙

↙表示回车。

GET / HTTP/1.0
查询方法名,有GET和POST
GET方法不支持消息体
服务器资源 HTTP版本

在telnet中测试HTTP的POST是很麻烦的事。这里就不讲了,何况GET和POST很类似。

处理

Baidu服务器根据请求,处理请求。如果用的是Servlet,就是执行那个Servlet的doGet方法。

响应



HTTP/1.1 200 OK
Date: Sun, 03 Feb 2008 08:08:44 GMT
Server: BWS/1.0
Content-Length: 3022
Content-Type: text/html
Cache-Control: private
Expires: Sun, 03 Feb 2008 08:08:44 GMT
Set-Cookie: BAIDUID=2BB1B837C8EC6794A132C5D8DDD76721:FG=1; expires=Sun, 03-Feb-38 08:08:44 GMT; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "

<html><head><meta http-equiv=Content-Type content="text/html;charset=gb2312"><title>百度一下,你就知道 </title><style>body{margin:4px 0}p{margin:0;padding:0}img{border:0}td,p,#u{font-size:12px}#b,#u,#l td,a{font-family:arial}#kw{font:16px Verdana;height:1.78em;padding-top:2px}#b{height:30px;padding-top:4px}#b,#b a{color:#77c;font-size:12px}#u{padding-right:10px;line-height:19px;text-align:right;margin-bottom:3px !important;margin-bottom:10px}#sb{height:2em;width:5.6em}#km{height:50px}#km a{font-family:宋体}#l{margin-bottom:5px}#m{margin-left:100px;width:22.1em;text-align:center}p,#b,table{width:600px;border:0}#sb,#km,#l,#m{font-size:14px}#m a,#m b{margin-right:1.14em}a{color:#00c}a:active{color:#f60}</style></head>
<body><div id=u></div><center><a href=http://hi.baidu.com/baidu target=_blank><img src=http://www.baidu.com/img/logo.gif width=174 height=59 alt="点此进入 百度空间"></a><br><br><br><br><table cellpadding=0 cellspacing=0 id=l><tr><td align=left><div id=m><a onclick=s(this) href=http://news.baidu.com>新&nbsp;闻</a><b>网&nbsp;页</b><a onclick=s(this) href=http://tieba.baidu.com>贴&nbsp;吧</a><a onclick=s(this) href=http://zhidao.baidu.com>知&nbsp;道</a><a onclick=s(this) href=http://mp3.baidu.com>MP3</a><a onclick=s(this) href=http://image.baidu.com>图&nbsp;片</a></div></td></tr></table>
<table cellpadding=0 cellspacing=0><tr valign=top><td width=92></td><td height=62 nowrap><form name=f action=/s><input type=text name=wd id=kw size=36 maxlength=100><script>var w=document.f.wd;w.focus();document.getElementById("u").innerHTML='<a href="http://passport.baidu.com/?login&tpl=mn&u='+escape(location.href)+'">登录</a>';function s(o){if(w.value.length>0){var h=o.href;var q=encodeURIComponent(w.value);if(h.indexOf("q=")!=-1){o.href=h.replace(new RegExp("q=[^&$]*"),"q="+q)}else{o.href+="?q="+q}}};(function(){if(new RegExp("q=([^&]+)").test(location.search)){w.value=decodeURIComponent(RegExp.$1)}})()</script><input type=hidden name=cl value=3> <input type=submit value=百度一下 id=sb><br><br></form></td><td width=100><a href=/search/jiqiao.html>帮助</a><br><a href=/gaoji/advanced.html>高级</a></td></tr></table>
<p id=km>&nbsp;<a href=http://hi.baidu.com>空间</a>&nbsp;|&nbsp;<a href=http://www.baidu.com/more>更多>></a></p>
<p style=height:60px></p>
<p style=height:30px><a onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href=http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com>把百度设为首页</a></p><p style=height:14px><a href=http://jingjia.baidu.com>企业推广</a> | <a href=http://top.baidu.com>搜索风云榜</a> | <a href=/home.html>关于百度</a> | <a href=http://ir.baidu.com>About Baidu</a></p><p id=b>&copy;2008 Baidu <a href=http://www.baidu.com/duty>使用百度前必读</a> <a href=http://www.miibeian.gov.cn target=_blank>京ICP证030173号</a> <a href=http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412><img src=http://gimg.baidu.com/img/gs.gif></a></p></center></body></html><!--4eca38de351397c2-->

失去了跟主机的连接。

终止

用telnet连接web服务器,连接会立马终止。如果用浏览器访问,浏览器会取得与页面有关的所有图像、样式表和脚本文件等,才终止连接。

HTTP查询和响应的消息格式

HTTP查询和响应的消息格式差不多。

格式
initliaze line
key1:value1
key2:value2

[Optional message body]
查询格式
GET /newaddr?name=jack&email=2 HTTP/1.0
accept:*/*
accept-language:zh-cn
... ...


响应格式
(具体见上面)
HTTP/1.1 200 OK
Date: Sun, 03 Feb 2008 08:08:44 GMT
Server: BWS/1.0
... ...

<html>...</html>

后记

HTTP定义了查询和响应的消息格式。但每个浏览器和服务器在这种格式下,有自己定义的内容。服务器通过浏览器请求的消息,知道浏览器具体是什么型号的,是什么操作系统。这些具体怎么做的,下文分解

posted on 2008-02-04 18:57 李四飞刀 阅读(1756) 评论(0)  编辑  收藏 所属分类: Struts2

只有注册用户登录后才能发表评论。


网站导航: