请求行
格式:请求方式 资源路径 HTTP版本号
举例:GET /test.html HTTP/1.1
请求方式:POST、HEAD、OPTIONS、DELETE、TRACE、PUT
状态行
格式:HTTP版本号 状态码 原因叙述
举例:HTTP/1.1 200 OK
利用telnet工具测试http请求行与状态行(首先启动tomcat服务器)
咱们向tomcat的主页面 /index.jsp发送请求
这里介绍一下telnet的用法
注:如果此处提示telnet为不可识别的命令,则需要到控制面板-->程序和功能-->打开和关闭windows功能-->打开telnet
功能,重启电脑后才能使用哦。
2.输入要连接的地址和端口 格式:telnet localhost 8080
telnet命令使用可通过 输入“telnet /help”查看使用说明
3.回车 如果连接成功则会跳转到
注:此时按住ctrl+]键打开屏幕回写功能,然后回车,跳到一个空屏幕。现在就可以发送请求了。
看一下HTTP/1.0和HTTP/1.1的区别
1.发送HTTP/1.0请求
注:第一行就是请求行,记得后面有一个回车换行符。
第三行开始是状态行HTTP/1.1 200 OK
第四行开始为消息头
最下面的为响应实体,也就是index.jsp的源代码
2.发送HTTP/1.1请求
注:此处会发现在返回的状态码中(HTTP/1.1 400 Bad Request)通过描述可以看出这是一次失败的请求,为什么呢?在上一章的笔记中提到了HTTP/1.0与HTTP/1.1的比较,
HTTP/1.1比HTTP/1.0多一个Host:头字段,而且这个头字段是必须的。
加上Host:头字段看结果
简单请求消息和响应消息
在HTTP协议中,还可以使用简单的请求消息和响应消息,它们都没有消息头部分。简单的请求消息只能是GET方式,
且请求行中不用HTTP版本号。对于简单的请求消息,服务器返回简单的响应消息,简单的响应消息只返回实体内容
注:发送简单请求消息之后获得简单回应消息后立马关闭连接!
对服务器发送带参数的GET和POST请求实例
我们利用tomcat给我们提供的例子
按照上面的方法发送get请求,请求的url地址为/examples/servlets/servlet/RequestParamExample?firstname=wang&lastname=wu
由于dos窗口的问题,在不回车换行的情况下,满行之后会覆盖前面的内容,不要惊慌,你继续写下去就行了!
发送get请求为
GET /examples/servlets/servlet/RequestParamExample?firstname=wang&lastname=wu HTTP/1.1
Host:
发送post请求
POST /examples/servlets/servlet/RequestParamExample HTTP/1.1
Host:
Content-Type: application/x-www-form-urlencoded
Content-Length: 26
firstname=wang&lastname=wu
注:在发送post请求时必须指定内容长度Content-Length:头字段,如果指定的长度小于实际长度,服务器会
自动截取制定长度,如果指定的长度大于实际长度,服务器不会立马发送请求,而是会等待用户输入直到满足
指定的长度(所以Content-Length:的值必须和实际长度一致)
这里找了一些Content-Type的值,仅供参考
常见文件http中content-type头值-(MIME类型)
.ppt – application/mspowerpoint
.ai – application/postscript
.aif – audio/x-aiff
.aifc – audio/x-aiff
.aiff – audio/x-aiff
.asc – text/plain
.au – audio/basic
.avi – video/x-msvideo
.bcpio – application/x-bcpio
.bin – application/octet-stream
.c – text/plain
.cc – text/plain
.ccad – application/clariscad
.cdf – application/x-netcdf
.class – application/octet-stream
.cpio – application/x-cpio
.cpt – application/mac-compactpro
.csh – application/x-csh
.css – text/css
.dcr – application/x-director
.dir – application/x-director
.dms – application/octet-stream
.doc – application/msword
.drw – application/drafting
.dvi – application/x-dvi
.dwg – application/acad
.dxf – application/dxf
.dxr – application/x-director
.eps – application/postscript
.etx – text/x-setext
.exe – application/octet-stream
.ez – application/andrew-inset
.f – text/plain
.f90 – text/plain
.fli – video/x-fli
.gif – image/gif
.gtar – application/x-gtar
.gz – application/x-gzip
.h – text/plain
.hdf – application/x-hdf
.hh – text/plain
.hqx – application/mac-binhex40
.htm – text/html
.html – text/html
.ice – x-conference/x-cooltalk
.ief – image/ief
.iges – model/iges
.igs – model/iges
.ips – application/x-ipscript
.ipx – application/x-ipix
.jpe – image/jpeg
.jpeg – image/jpeg
.jpg – image/jpeg
.js – application/x-javascript
.kar – audio/midi
.latex – application/x-latex
.lha – application/octet-stream
.lsp – application/x-lisp
.lzh – application/octet-stream
.m – text/plain
.man – application/x-troff-man
.me – application/x-troff-me
.mesh – model/mesh
.mid – audio/midi
.midi – audio/midi
.mif – application/vnd.mif
.mime – www/mime
.mov – video/quicktime
.movie – video/x-sgi-movie
.mp2 – audio/mpeg
.mp3 – audio/mpeg
.mpe – video/mpeg
.mpeg – video/mpeg
.mpg – video/mpeg
.mpga – audio/mpeg
.ms – application/x-troff-ms
.msh – model/mesh
.nc – application/x-netcdf
.oda – application/oda
.pbm – image/x-portable-bitmap
.pdb – chemical/x-pdb
.pdf – application/pdf
.pgm – image/x-portable-graymap
.pgn – application/x-chess-pgn
.png – image/png
.pnm – image/x-portable-anymap
.pot – application/mspowerpoint
.ppm – image/x-portable-pixmap
.pps – application/mspowerpoint
.ppt – application/mspowerpoint
.ppz – application/mspowerpoint
.pre – application/x-freelance
.prt – application/pro_eng
.ps – application/postscript
.qt – video/quicktime
.ra – audio/x-realaudio
.ram – audio/x-pn-realaudio
.ras – image/cmu-raster
.rgb – image/x-rgb
.rm – audio/x-pn-realaudio
.roff – application/x-troff
.rpm – audio/x-pn-realaudio-plugin
.rtf – text/rtf
.rtx – text/richtext
.scm – application/x-lotusscreencam
.set – application/set
.sgm – text/sgml
.sgml – text/sgml
.sh – application/x-sh
.shar – application/x-shar
.silo – model/mesh
.sit – application/x-stuffit
.skd – application/x-koan
.skm – application/x-koan
.skp – application/x-koan
.skt – application/x-koan
.smi – application/smil
.smil – application/smil
.snd – audio/basic
.sol – application/solids
.spl – application/x-futuresplash
.src – application/x-wais-source
.step – application/STEP
.stl – application/SLA
.stp – application/STEP
.sv4cpio – application/x-sv4cpio
.sv4crc – application/x-sv4crc
.swf – application/x-shockwave-flash
.t – application/x-troff
.tar – application/x-tar
.tcl – application/x-tcl
.tex – application/x-tex
.texi – application/x-texinfo
.texinfo – application/x-texinfo
.tif – image/tiff
.tiff – image/tiff
.tr – application/x-troff
.tsi – audio/TSP-audio
.tsp – application/dsptype
.tsv – text/tab-separated-values
.txt – text/plain
.unv – application/i-deas
.ustar – application/x-ustar
.vcd – application/x-cdlink
.vda – application/vda
.viv – video/vnd.vivo
.vivo – video/vnd.vivo
.vrml – model/vrml
.wav – audio/x-wav
.wrl – model/vrml
.xbm – image/x-xbitmap
.xlc – application/vnd.ms-excel
.xll – application/vnd.ms-excel
.xlm – application/vnd.ms-excel
.xls – application/vnd.ms-excel
.xlw – application/vnd.ms-excel
.xml – text/xml
.xpm – image/x-xpixmap
.xwd – image/x-xwindowdump
.xyz – chemical/x-pdb
.zip – application/zip
响应状态码
响应状态码用于表示服务器对请求的各种不同处理结果和状态,它是一个三位的十进制数。响应状态码可归为
5种类别,适用最高位为1到5来进行分类,如下所示:
(1)100~199
表示成功接收请求,要求客户端继续提交下一次请求才能完成整个处理过程。
(2)200~299
表示成功接收请求并已完成整个处理过程。
(3)300~399
为完成请求,客户需进一步细化请求,例如,请求的资源已移动一个新地址
(4)400~499
客户端的请求有错误
(5)500~599
服务器出现错误