1、什麼是HTTP
telnet不能使用:點擊打開鏈接
安裝IE瀏覽器插件HttpWatch,查看IE瀏覽器通過HTTP協議獲取某個頁面。
2、HTTP1.0與1.1的區別
3、HTTP請求
一個請求行、請求頭、請求題(form),如下所示 :
4、HTTP請求行
(1)、如請求方式爲GET方式,則可以在請求的URL地址後以?的形式帶上交給服務器的數據,多個數據之間以&進行分隔,例如:GET /mail/1.html?name=abc&password=xyz HTTP/1.1 <a href="a/2.html?name=aaa&password=123">點點</a>GET方式的特點:在URL地址後附帶的參數是有限制的,其數據容量通常不能超過1K。不能是中文(2)、如請求方式爲POST方式,則可以在請求的實體內容中向服務器發送數據,Post方式的特點:傳送的數據量無限制內容在實體內容中!
5、HTTP請求頭
Accept-Encoding:瀏覽器通過這個頭告訴服務器,支持的壓縮格式 Accept-Encoding: gzip,compress
Accept-Language:瀏覽器通過這個頭告訴服務器,它的語言環境 Accept-Language: en-us,zh-cn
Host:瀏覽器通過這個頭告訴服務器,想訪問哪臺主機 Host: www.it315.org:80
If-Modified-Since: 瀏覽器通過這個頭告訴服務器,緩存數據的時間 If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT
Referer:瀏覽器通過這個頭告訴服務器,客戶機是哪個頁面來的 防盜鏈 Referer: http://www.it315.org/index.jsp
Connection:瀏覽器通過這個頭告訴服務器,請求完後是斷開鏈接還是何持鏈接 Connection: close/Keep-Alive
6、HTTP響應
一個狀態行、若干消息頭、以及實體內容 。
7、HTTP狀態行
8、HTTP狀態頭
Server:服務器通過這個頭,告訴瀏覽器服務器的型號 Server:apache tomcat
Content-Encoding:服務器通過這個頭,告訴瀏覽器,數據的壓縮格式 Content-Encoding: gzip
Content-Length: 服務器通過這個頭,告訴瀏覽器回送數據的長度 Content-Length: 80
Content-Language: 服務器通過這個頭,告訴瀏覽器語言環境 Content-Language: zh-cn
Content-Type:服務器通過這個頭,告訴瀏覽器回送數據的類型 Content-Type: text/html; charset=GB2312
Refresh:服務器通過這個頭,告訴瀏覽器定時刷新(聊天室) Refresh: 1;url=http://www.it315.org
Content-Disposition: 服務器通過這個頭,告訴瀏覽器以下載方式打數據 Content-Disposition: attachment; filename=aaa.zip
Transfer-Encoding:服務器通過這個頭,告訴瀏覽器數據是以分塊方式回送的 Transfer-Encoding: chunked
Expires: -1 控制瀏覽器不要緩存(也可以寫時間,緩存到什麼時候) Expires: -1
Cache-Control: no-cache
Pragma: no-cache
//數據壓縮
public void test2(HttpServletRequest request, HttpServletResponse response) throws IOException{
String data = "abcdabcdab";
System.out.println("原始數據的大小爲:" + data.getBytes().length);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(bout); //buffer
gout.write(data.getBytes());
gout.close();
//得到壓縮後的數據
byte g[] = bout.toByteArray();
response.setHeader("Content-Encoding", "gzip");
response.setHeader("Content-Length",g.length +"");
response.getOutputStream().write(g);
}
回送圖片數據:
//指定回送數據類型
public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException{
//C:\apache-tomcat-6.0.20\conf\web.xml
response.setHeader("content-type", "image/jpeg");
InputStream in = this.getServletContext().getResourceAsStream("/1.jpg");
byte buffer[] = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
}
//指定瀏覽器定時刷新
public void test4(HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setHeader("refresh", "3;url='http://www.sina.com'");
response.getWriter().write("abcd");
}
指定下載:
//指定瀏覽器下載
public void test5(HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setHeader("content-disposition", "attachment;filename=xxx.jpg");
InputStream in = this.getServletContext().getResourceAsStream("/2.jpg");
byte buffer[] = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
}