10-01
16

HTTP的请求头

HTTP是一种请求/回应协议,用户通过客户端(比如IE)发送给网站一个请求,那么网站会返回一个相应,在发送请求信息之前,会发送一些头文件,也就是HTTP的请求头,这里我们用一个叫HTTP Analyzer的软件来截获一下我们访问网站时的HTTP头

view plaincopy to clipboardprint?
<headers>  
      <requestheaders>  
        <header>GET / HTTP/1.1</header>  
        <header>Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, */*</header>  
        <header>Accept-Language: zh-cn</header>  
        <header>User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)</header>  
        <header>Accept-Encoding: gzip, deflate</header>  
        <header>Host: www.baidu.com</header>  
        <header>Connection: Keep-Alive</header>  
        <header>Cookie: BAIDUID=A1133C309AE227BC36AA18E02FC2B091:FG=1; BDLFONT=0</header>  
      </requestheaders>  
      <responseheaders>  
        <header>HTTP/1.1 200 OK</header>  
        <header>Date: Fri, 16 Oct 2009 10:44:50 GMT</header>  
        <header>Server: BWS/1.0</header>  
        <header>Content-Length: 1744</header>  
        <header>Content-Type: text/html;charset=gb2312</header>  
        <header>Cache-Control: private</header>  
        <header>Expires: Fri, 16 Oct 2009 10:44:50 GMT</header>  
        <header>Content-Encoding: gzip</header>  
      </responseheaders>  
    </headers>  
<headers>
      <requestheaders>
        <header>GET / HTTP/1.1</header>
        <header>Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, */*</header>
        <header>Accept-Language: zh-cn</header>
        <header>User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)</header>
        <header>Accept-Encoding: gzip, deflate</header>
        <header>Host: www.baidu.com</header>
        <header>Connection: Keep-Alive</header>
        <header>Cookie: BAIDUID=A1133C309AE227BC36AA18E02FC2B091:FG=1; BDLFONT=0</header>
      </requestheaders>
      <responseheaders>
        <header>HTTP/1.1 200 OK</header>
        <header>Date: Fri, 16 Oct 2009 10:44:50 GMT</header>
        <header>Server: BWS/1.0</header>
        <header>Content-Length: 1744</header>
        <header>Content-Type: text/html;charset=gb2312</header>
        <header>Cache-Control: private</header>
        <header>Expires: Fri, 16 Oct 2009 10:44:50 GMT</header>
        <header>Content-Encoding: gzip</header>
      </responseheaders>
    </headers>  

Request Header

1. GET / HTTP/1.1

一般情况下请求一个网址或是打开标签页等等基本都是Get请求,涉及到提交一些数据会比较通用Post请求,而HTTP/1.1则是协议,基本上是由浏览器的一些属性决定使用哪种协议,大多数情况下都是HTTP/1.0或者HTTP/1.1

2.Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, */*

基本上就是告诉用户浏览器有能力处理哪种文件类型.

3.Accept-Language: zh-cn

告诉服务器比较喜欢接受哪种类型的文档,如果服务器有能力的话.

4.User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)

描述浏览器的类型,包括操作系统的版本或者一些附加信息等等.

5.Accept-Encoding: gzip, deflate

描述浏览器处理文件压缩传输的方式.

6.Host: www.baidu.com

描述请求的主机名称.

7.Connection: Keep-Alive

Keep-Alive功能使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接.

8.Cookie: BAIDUID=A1133C309AE227BC36AA18E02FC2B091:FG=1; BDLFONT=0

Cookie这个东西基本就是保存了服务器能识别的关于客户本身的一些信息,可能在以后得请求中还会用到这个信息.


Response Header

1.HTTP/1.1 200 OK

前面的也是协议,200代表的是访问的网页正常.

2.Date: Fri, 16 Oct 2009 10:44:50 GMT

服务器处理的时间.

3.Server: BWS/1.0

服务器类型以及版本.

4.Content-Length: 1744

从空行以后的内容算起,以字节为单位,浏览器接收到它所指定的字节数的内容以后就会认为这个消息已经被完整接收了.

5.Content-Type: text/html;charset=gb2312

返回的内容形式和编码格式

6.Cache-Control: private

打开新窗口如果指定cache-control的值为private、no-cache、must-revalidate,那么打开新窗口访问时都会重新访问服务器.

7.Content-Encoding: gzip

传送文本可以接受的压缩形式.

其实我们可以用代码模拟发送HTTP消息头给Web Server,然后看看Web Server返回的内容是什么.Java代码如下:

view plaincopy to clipboardprint?
package org.ring09h.test;  
  
import java.io.BufferedReader;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.OutputStream;  
import java.net.Socket;  
  
public class SendHttpTest {  
    public static void main(String[] args) throws Exception {  
        Socket socket = new Socket("localhost", 8080);  
        OutputStream os = socket.getOutputStream();  
        InputStream is = socket.getInputStream();  
        StringBuffer sb = new StringBuffer();  
        String p;  
        sb.append("GET http://localhost:8080/Struts2Demo-Tags/ HTTP/1.1\r\n");  
        sb.append("Accept: */*\n");  
        sb.append("Accept-Charset: utf-8\n");  
        sb.append("Accept-Encoding: gzip\n");  
        sb.append("Accept-Language: zh_CN\n");  
        sb.append("Connection: Keep-Alive\n");  
        sb.append("Pragma: no-cache\n");  
        sb.append("User-Agent: (unknow)\n\n\n");  
        os.write(sb.toString().getBytes());  
        os.flush();  
        InputStreamReader in = new InputStreamReader(is);  
        BufferedReader buff = new BufferedReader(in);  
        while((p = buff.readLine()) != null) {  
            System.out.println(p);  
        }  
        os.close();  
        is.close();  
        in.close();  
        buff.close();  
        socket.close();  
    }  
}  
package org.ring09h.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class SendHttpTest {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("localhost", 8080);
        OutputStream os = socket.getOutputStream();
        InputStream is = socket.getInputStream();
        StringBuffer sb = new StringBuffer();
        String p;
        sb.append("GET http://localhost:8080/Struts2Demo-Tags/ HTTP/1.1\r\n");
        sb.append("Accept: */*\n");
        sb.append("Accept-Charset: utf-8\n");
        sb.append("Accept-Encoding: gzip\n");
        sb.append("Accept-Language: zh_CN\n");
        sb.append("Connection: Keep-Alive\n");
        sb.append("Pragma: no-cache\n");
        sb.append("User-Agent: (unknow)\n\n\n");
        os.write(sb.toString().getBytes());
        os.flush();
        InputStreamReader in = new InputStreamReader(is);
        BufferedReader buff = new BufferedReader(in);
        while((p = buff.readLine()) != null) {
            System.out.println(p);
        }
        os.close();
        is.close();
        in.close();
        buff.close();
        socket.close();
    }
}

程序输出:
view plaincopy to clipboardprint?
HTTP/1.1 200 OK  
Server: Apache-Coyote/1.1  
Set-Cookie: JSESSIONID=DB175D0F186F7ABEEDD9D6BEF82AEE38; Path=/Struts2Demo-Tags  
Content-Type: text/html;charset=UTF-8  
Content-Length: 835  
Date: Mon, 19 Oct 2009 07:33:35 GMT  
  
  
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <title>Get time</title>  
  </head>    
  <body>  
      
      
    <a href="/Struts2Demo-Tags/dateAction.action;jsessionid=DB175D0F186F7ABEEDD9D6BEF82AEE38" mce_href="Struts2Demo-Tags/dateAction.action;jsessionid=DB175D0F186F7ABEEDD9D6BEF82AEE38">Test</a><br>  
    <form id="loginAction" action="loginAction.action;jsessionid=DB175D0F186F7ABEEDD9D6BEF82AEE38" method="post">  
<table class="wwFormTable">  
        name:<tr>  
    <td class="tdLabel"></td>  
    <td  
><input type="text" name="user.name" value="" id="loginAction_user_name"/></td>  
</tr>  
  
        password:<tr>  
    <td class="tdLabel"></td>  
    <td  
><input type="password" name="user.password" id="loginAction_user_password"/></td>  
</tr>  
  
        <tr>  
    <td colspan="2"><div align="right"><input type="submit" id="loginAction_0" value="Submit"/>  
</div></td>  
</tr>  
  
    </table></form>  
  
  
  
  
  </body>  
</html>  
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=DB175D0F186F7ABEEDD9D6BEF82AEE38; Path=/Struts2Demo-Tags
Content-Type: text/html;charset=UTF-8
Content-Length: 835
Date: Mon, 19 Oct 2009 07:33:35 GMT



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Get time</title>
  </head>  
  <body>
    
    
    <a href="/Struts2Demo-Tags/dateAction.action;jsessionid=DB175D0F186F7ABEEDD9D6BEF82AEE38" mce_href="Struts2Demo-Tags/dateAction.action;jsessionid=DB175D0F186F7ABEEDD9D6BEF82AEE38">Test</a><br>
    <form id="loginAction" action="loginAction.action;jsessionid=DB175D0F186F7ABEEDD9D6BEF82AEE38" method="post">
<table class="wwFormTable">
        name:<tr>
    <td class="tdLabel"></td>
    <td
><input type="text" name="user.name" value="" id="loginAction_user_name"/></td>
</tr>

        password:<tr>
    <td class="tdLabel"></td>
    <td
><input type="password" name="user.password" id="loginAction_user_password"/></td>
</tr>

        <tr>
    <td colspan="2"><div align="right"><input type="submit" id="loginAction_0" value="Submit"/>
</div></td>
</tr>

    </table></form>




  </body>
</html>

以上代码遇到的问题:

1,在发送请求的时候一定要将Request head写全,我用的是Tomcat,可能不同的Web服务器要求的Request head不同,比如我上面的代码去掉User-agent,那么就没有Response了.但是网上有一些例子,是不要求User-agent的,具体我也没有尝试过.




文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 377
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭