目录

 

什么是 HTTP 协议?

HTTP的报文结构

HTTP请求报文

GET请求报文

POST请求报文 


什么是 HTTP 协议?

全称:hypertext transfer protocol(超文本传输协议),是 TCP/IP 协议族的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的过程。

通俗的讲,当客户端或者浏览器连接上服务器之后,若想获得 web 服务器中的 web 资源,就必须与 服务端进行通信,通信的双方必须能够解析发送的消息,那么这个消息就要符合一定的通讯格式,HTTP 就是用于定义双方的通信格式。

 

HTTP的报文结构

http的请求报文和响应报文的格式基本一样,主要分为三部分:

  1. 起始行(start line): 描述请求或响应的状态
  2. 头部字段(header): 以 key:value 的形式展示
  3. 数据实体(entity/ body) :实际要传输的数据,可以是文本,也可以是图片、文件、视频等二进制数据

其中 "起始行"  和 "头部字段" 又一起被称为 “请求头” 或 “响应头” ,消息正文又被称为 “实体(entity)” 或者 与 “header” 相对应的 “body” 。 请求头和响应头与 “body” 中间有一个空行(CRLF)作为区分两者的边界,记住是第一个空行(CRLF),如果有多个空行,那么其他的空行则被归为“body”。所以一个完整的报文如下图所示(图是盗来的(#^.^#)):

HTTP报文是什么样的?

HTTP请求报文

GET请求报文

我们先来看下一个普通的GET请求,它的报文结构是什么样的:

GET  /books/java.html  HTTP/1.1    ---请求行(如果有带参数,会拼接在URL)
-------------------------------------------------------
Accept: */*                                     ---header
Accept-Language: en-us 
Connection: Keep-Alive 
Host: localhost  
Referer: http://localhost/links.asp 
User-Agent: Mozilla/4.0 
Accept-Encoding: gzip, deflate 
-------------------------------------------------------
                                               ---空白行
                                               --body(这里没有内容)

“虚线” 是我为了清楚将三部分分开加上去的,实际请求是没有的。 我们照着HTTP报文结构来看下GET请求的报文:

1. 起始行

GET  /books/java.html  HTTP/1.1

起始行包括三个部分   请求方式(GET)+ 请求的资源在服务器中的相对路径(URI) + HTTP的版本  ,所以该起始行表示的是,使用HTTP 1.1 版本协议的标准,用GET请求方式,请求 目标服务器 “/books/java.html” 路径下的资源,至于目标服务器的IP地址和端口号呢,这个参数就在header中,“Host: localhost” ,所以请求的服务器完整地址是  http://locallhost:8080/books/java.html  因为我是在本地的tomcat服务器上访问的,所以ip地址是localhost,相当于平常看到的192.168.3.56 这种地址。

 

2. header  

header中既有固定的参数字段(属于HTTP协议),也可以根据自己的需求添加。我们来看下

Accept: */*                         -- 可接收的数据类型
Accept-Language: en-us  -- 支持的语言设置
Connection: Keep-Alive    -- 保持连接  
Host: localhost                  -- 请求的资源所在的地址 
Referer: http://localhost/links.asp  --表示从哪个页面发起请求
User-Agent: Mozilla/4.0    -- 发起请求的浏览器名和版本号
Accept-Encoding: gzip, deflate  --支持的编解码格式

 Host 字段只能出现在请求头中,而且是必须出现的,否则就是一个错误的报文。Host 字段告诉服务器这个请求应该由哪个主机来处理,当一台计算机上托管了多个虚拟主机的时候,服务器端就需要用 Host 字段来选择。

更多的header参数,详见:常用的HTTP请求头与响应头

 

3. body

GET 请求是没有“body”这部分参数的,GET 请求如果带有参数,参数会直接拼接在 start line 的第二个参数后面,用 "?" 隔开,以key=value的形式添加参数,参数与参数之间用 "&" 隔开。如:

GET  /books/java.html?name=shonn&password=123456  HTTP/1.1 

POST请求报文 

1、发送纯文本的参数

POST /foo.php HTTP/1.1

---------------------------------------------------------------------------------

Host: localhost

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5)Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Referer: http://localhost/test.php

Content-Type: application/x-www-form-urlencoded     ---GET请求没有这个

Content-Length: 43

---------------------------------------------------------------------------------

first_name=John&last_name=Doe&action=Submit  -----内容实体

2、发送文本参数和文件

POST /hello/checkUser.html?opt=xxx HTTP/1.1

Accept: */*

Referer: http://localhost:8000/hello/index.html

Accept-Language: zh-cn

Content-Type: multipart/form-data; boundary=---------------------------7d931c5d043e

Accept-Encoding: gzip, deflate

Host: localhost:8000

Content-Length: 382

Connection: Keep-Alive

Cache-Control: no-cache

Cookie: JSESSIONID=6FE3D8E365DF9FE26221A32624470D24

 

-----------------------------7d931c5d043e

Content-Disposition: form-data; name="username"

 

zxy

-----------------------------7d931c5d043e

Content-Disposition: form-data; name="age"

 

25

-----------------------------7d931c5d043e

Content-Disposition: form-data; name="file"; filename="C:/111.txt"

Content-Type: text/plain

 

hello

-----------------------------7d931c5d043e--

 

POST请求和GET请求报文上的区别,主要在于POST请求参数是放在 “body” 中。

“Content-Type” 表示请求体的MIME类型,通俗的讲就是表示发送的数据的格式,“ContentLength” 表示的是数据的长度。常用的 Content-Type :

(1)application/x-www-form-urlencoded窗体数据被编码为名称/值对。这是标准的编码格式,为默认的方式。

(2)multipart/form-data窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。二进制数据传输方式,该格式会生成一个boundary字符串来分割不同的参数,格式为:boundary=--${boundary},一般是以两个开头,添加到header的Content-Type中,如 Content-Type: multipart/form-data; boundary=---------------------------7d931c5d043e

multipart/form-data 经常用来传送参数和文件混合的数据,Content-Disposition的格式为:

Content-Disposition: “disposition-type” *( ";" disposition-parm )

Content-Disposition为属性名,disposition-type是以什么方式下载,如

  •  "attachment" 表示以附件方式下载
  •  "inline" 用网页打开,
  •  "form-data" 文件上传使用

*( ";" disposition-parm ) 表示可带有多个参数,用分号隔开

 

例如:上传普通参数:Content-Disposition: form-data; name="username"

上传文件:Content-Disposition: form-data; name="file"; filename="C:/1.mp4"

  • name:上传的参数名
  • filename:上传的文件名,可以是绝对路径也可以只是文件名,如果文件名为中文,要先进行编码,例如fileName = new String(fileName.getBytes(), "UTF-8");

(3)text/plain:窗体数据以纯文本形式进行编码,经常在参数后加上Charset表示文本内容的编码,如:Content-Type: text/plain;Charset=UTF-8 ,其中不含任何控件或格式字符。

附:HTTP MIME类型即ContentType属性值列表

MIME-Typ

Dateiendung(en)

Bedeutung

application/acad

*.dwg

AutoCAD-Dateien (nach NCSA)

application/applefile

 

AppleFile-Dateien

application/astound

*.asd *.asn

Astound-Dateien

application/dsptype

*.tsp

TSP-Dateien

application/dxf

*.dxf

AutoCAD-Dateien (nach CERN)

application/futuresplash

*.spl

Flash Futuresplash-Dateien

application/gzip

*.gz

GNU Zip-Dateien

application/listenup

*.ptlk

Listenup-Dateien

application/mac-binhex40

*.hqx

Macintosh Binärdateien

application/mbedlet

*.mbd

Mbedlet-Dateien

application/mif

*.mif

FrameMaker Interchange Format Dateien

application/msexcel

*.xls *.xla

Microsoft Excel Dateien

application/mshelp

*.hlp *.chm

Microsoft Windows Hilfe Dateien

application/mspowerpoint

*.ppt *.ppz *.pps *.pot

Microsoft Powerpoint Dateien

application/msword

*.doc *.dot

Microsoft Word Dateien

application/octet-stream

*.bin *.exe *.com *.dll *.class

Ausführbare Dateien

application/oda

*.oda

Oda-Dateien

application/pdf

*.pdf

Adobe PDF-Dateien

application/postscript

*.ai *.eps *.ps

Adobe PostScript-Dateien

application/rtc

*.rtc

RTC-Dateien

application/rtf

*.rtf

Microsoft RTF-Dateien

application/studiom

*.smp

Studiom-Dateien

application/toolbook

*.tbk

Toolbook-Dateien

application/vocaltec-media-desc

*.vmd

Vocaltec Mediadesc-Dateien

application/vocaltec-media-file

*.vmf

Vocaltec Media-Dateien

application/xhtml+xml

*.htm *.html *.shtml *.xhtml

XHTML-Dateien

application/xml

*.xml

XML-Dateien

application/x-bcpio

*.bcpio

BCPIO-Dateien

application/x-compress

*.z

zlib-komprimierte Dateien

application/x-cpio

*.cpio

CPIO-Dateien

application/x-csh

*.csh

C-Shellscript-Dateien

application/x-director

*.dcr *.dir *.dxr

Macromedia Director-Dateien

application/x-dvi

*.dvi

DVI-Dateien

application/x-envoy

*.evy

Envoy-Dateien

application/x-gtar

*.gtar

GNU tar-Archivdateien

application/x-hdf

*.hdf

HDF-Dateien

application/x-httpd-php

*.php *.phtml

PHP-Dateien

application/x-javascript

*.js

serverseitige JavaScript-Dateien

application/x-latex

*.latex

LaTeX-Quelldateien

application/x-macbinary

*.bin

Macintosh Binärdateien

application/x-mif

*.mif

FrameMaker Interchange Format Dateien

application/x-netcdf

*.nc *.cdf

Unidata CDF-Dateien

application/x-nschat

*.nsc

NS Chat-Dateien

application/x-sh

*.sh

Bourne Shellscript-Dateien

application/x-shar

*.shar

Shell-Archivdateien

application/x-shockwave-flash

*.swf *.cab

Flash Shockwave-Dateien

application/x-sprite

*.spr *.sprite

Sprite-Dateien

application/x-stuffit

*.sit

Stuffit-Dateien

application/x-supercard

*.sca

Supercard-Dateien

application/x-sv4cpio

*.sv4cpio

CPIO-Dateien

application/x-sv4crc

*.sv4crc

CPIO-Dateien mit CRC

application/x-tar

*.tar

tar-Archivdateien

application/x-tcl

*.tcl

TCL Scriptdateien

application/x-tex

*.tex

TeX-Dateien

application/x-texinfo

*.texinfo *.texi

Texinfo-Dateien

application/x-troff

*.t *.tr *.roff

TROFF-Dateien (Unix)

application/x-troff-man

*.man *.troff

TROFF-Dateien mit MAN-Makros (Unix)

application/x-troff-me

*.me *.troff

TROFF-Dateien mit ME-Makros (Unix)

application/x-troff-ms

*.me *.troff

TROFF-Dateien mit MS-Makros (Unix)

application/x-ustar

*.ustar

tar-Archivdateien (Posix)

application/x-wais-source

*.src

WAIS Quelldateien

application/x-www-form-urlencoded

 

HTML-Formulardaten an CGI

application/zip

*.zip

ZIP-Archivdateien

audio/basic

*.au *.snd

Sound-Dateien

audio/echospeech

*.es

Echospeed-Dateien

audio/tsplayer

*.tsi

TS-Player-Dateien

audio/voxware

*.vox

Vox-Dateien

audio/x-aiff

*.aif *.aiff *.aifc

AIFF-Sound-Dateien

audio/x-dspeeh

*.dus *.cht

Sprachdateien

audio/x-midi

*.mid *.midi

MIDI-Dateien

audio/x-mpeg

*.mp2

MPEG-Dateien

audio/x-pn-realaudio

*.ram *.ra

RealAudio-Dateien

audio/x-pn-realaudio-plugin

*.rpm

RealAudio-Plugin-Dateien

audio/x-qt-stream

*.stream

Quicktime-Streaming-Dateien

audio/x-wav

*.wav

WAV-Dateien

drawing/x-dwf

*.dwf

Drawing-Dateien

image/cis-cod

*.cod

CIS-Cod-Dateien

image/cmu-raster

*.ras

CMU-Raster-Dateien

image/fif

*.fif

FIF-Dateien

image/gif

*.gif

GIF-Dateien

image/ief

*.ief

IEF-Dateien

image/jpeg

*.jpeg *.jpg *.jpe

JPEG-Dateien

image/png

*.png

PNG-Dateien

image/tiff

*.tiff *.tif

TIFF-Dateien

image/vasa

*.mcf

Vasa-Dateien

image/vnd.wap.wbmp

*.wbmp

Bitmap-Dateien (WAP)

image/x-freehand

*.fh4 *.fh5 *.fhc

Freehand-Dateien

image/x-icon

*.ico

Icon-Dateien (z.B. Favoriten-Icons)

image/x-portable-anymap

*.pnm

PBM Anymap Dateien

image/x-portable-bitmap

*.pbm

PBM Bitmap Dateien

image/x-portable-graymap

*.pgm

PBM Graymap Dateien

image/x-portable-pixmap

*.ppm

PBM Pixmap Dateien

image/x-rgb

*.rgb

RGB-Dateien

image/x-windowdump

*.xwd

X-Windows Dump

image/x-xbitmap

*.xbm

XBM-Dateien

image/x-xpixmap

*.xpm

XPM-Dateien

message/external-body

 

Nachricht mit externem Inhalt

message/http

 

HTTP-Headernachricht

message/news

 

Newsgroup-Nachricht

message/partial

 

Nachricht mit Teilinhalt

message/rfc822

 

Nachricht nach RFC 2822

model/vrml

*.wrl

Visualisierung virtueller Welten (VRML)

multipart/alternative

 

mehrteilige Daten gemischt

multipart/byteranges

 

mehrteilige Daten mit Byte-Angaben

multipart/digest

 

mehrteilige Daten / Auswahl

multipart/encrypted

 

mehrteilige Daten verschlüsselt

multipart/form-data

 

mehrteilige Daten aus HTML-Formular (z.B. File-Upload)

multipart/mixed

 

mehrteilige Daten gemischt

multipart/parallel

 

mehrteilige Daten parallel

multipart/related

 

mehrteilige Daten / verbunden

multipart/report

 

mehrteilige Daten / Bericht

multipart/signed

 

mehrteilige Daten / bezeichnet

multipart/voice-message

 

mehrteilige Daten / Sprachnachricht

text/comma-separated-values

*.csv

kommaseparierte Datendateien

text/css

*.css

CSS Stylesheet-Dateien

text/html

*.htm *.html *.shtml

HTML-Dateien

text/javascript

*.js

JavaScript-Dateien

text/plain

*.txt

reine Textdateien

text/richtext

*.rtx

Richtext-Dateien

text/rtf

*.rtf

Microsoft RTF-Dateien

text/tab-separated-values

*.tsv

tabulator-separierte Datendateien

text/vnd.wap.wml

*.wml

WML-Dateien (WAP)

application/vnd.wap.wmlc

*.wmlc

WMLC-Dateien (WAP)

text/vnd.wap.wmlscript

*.wmls

WML-Scriptdateien (WAP)

application/vnd.wap.wmlscriptc

*.wmlsc

WML-Script-C-dateien (WAP)

text/xml

*.xml

XML-Dateien

text/xml-external-parsed-entity

 

extern geparste XML-Dateien

text/x-setext

*.etx

SeText-Dateien

text/x-sgml

*.sgm *.sgml

SGML-Dateien

text/x-speech

*.talk *.spc

Speech-Dateien

video/mpeg

*.mpeg *.mpg *.mpe

MPEG-Dateien

video/quicktime

*.qt *.mov

Quicktime-Dateien

video/vnd.vivo

*.viv *.vivo

Vivo-Dateien

video/x-msvideo

*.avi

Microsoft AVI-Dateien

video/x-sgi-movie

*.movie

Movie-Dateien

workbook/formulaone

*.vts *.vtts

FormulaOne-Dateien

x-world/x-3dmf

*.3dmf *.3dm *.qd3d *.qd3

3DMF-Dateien

x-world/x-vrml

*.wrl

Visualisierung virtueller Welten (VRML) (veralteter MIME-Typ, aktuell ist model/vrml)

 

相关文章:

  • 2022-01-04
  • 2021-09-13
  • 2021-06-25
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
  • 2023-03-11
猜你喜欢
  • 2022-12-23
  • 2022-01-09
  • 2021-11-29
  • 2022-12-23
  • 2021-10-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案