一、http请求的格式:
<request line> //request type,http version, resource requested
<headers> //additional infomation server site used
<blank line> //
<request body> //add any info here
HTTP定义了与服务器交互的方法,get 和 post。(还有put、delete)用于编码和传递变量名/变量值对,并使用相关的语义
get,以urlencoding编码,以查询字符串的方式传递参数。 server site:Request.QueryString[], length limited 1K, security issue
post,也以url编码,但参数放在http请求内部、 server site: Request.Form[], no length limited, no issue about security
二、Get请求的实例
GET /cgi-bin/tech/method.cgi?txtGET=TestGetMethod HTTP/1.1 //request line
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*Referer: Accept-Language: zh-cnAccept-Encoding: gzip, deflateUser-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)Host: localhost:8080Connection: Keep-Alive //headers end发出请求的html代码:
<form action="http://localhost:8080/cgi-bin/tech/method.cgi" method="GET"> //method为get时,浏览器会忽略url?后面的内容
<input type="text" size="10" value="TestGetMethod" name="txtGET"><input type=submit value="GET方式"></form>浏览器会自动将各表单字段附加在request line中的url后面。客户端的浏览器会缓存他。
三、Post请求的实例
POST /cgi-bin/tech/method.cgi HTTP/1.1 //request line
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms- //headers beginpowerpoint, application/vnd.ms-excel, application/msword, */*Referer: Accept-Language: zh-cnContent-Type: application/x-www-form-urlencodedAccept-Encoding: gzip, deflateUser-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)Host: localhost:8080Content-Length: 22Connection: Keep-Alive //headers endtxtPOST=TestPOSTMethod //body line
发送请求的代码
<form action="http://localhost:8080/cgi-bin/tech/method.cgi" method="POST">
<input type="text" size="10" value="TestPOSTMethod" name="txtPOST"><input type=submit value="POST方式"></form>浏览器会把各表单元素及数据作为http body内容发送给后台。
常见的还有post/get 混发。其实,method=“POST”,只是url后面加了"?parameterName=parameterValue"