[java] 理解http

作者: linzebingo | 来源:发表于2017-01-05 09:40 被阅读32次

    0. 先明晰几个概念

    • tcp/ip:是传输层协议,主要解决数据如何在网络中传输
    • http:是应用层的协议,规定了如何包装数据。应用层还有很多协议,比如FTP、TELNET 等
    • socket:是对tcp/ip 协议的封装,socket 本身并不是协议,而是一个调用接口(API),通过socket,我们才能使用tcp/ip 协议

    就是人们<big>约定</big>了一种格式的网络请求就是http请求。

    Question
    1.http 这一种约定的格式是怎么样的呢?
    2.这么说socket 也可以来发http 请求咯?

    1.示例:一个常见的http请求和响应

    1.1 HTTP 请求

    一个 HTTP 请求包括三个组成部分:

    • 方法—统一资源标识符(URI)—协议/版本
    • 请求的头部
    • 主体内容

    下面是一个 HTTP 请求的例子:
    POST /examples/default.jsp HTTP/1.1
    Accept: text/plain; text/html
    Accept-Language: en-gb
    Connection: Keep-Alive
    Host: localhost
    User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
    Content-Length: 33
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip, deflate

    lastName=Franks&firstName=Michael

    说明:

    • 方法—统一资源标识符(URI)—协议/版本出现在请求的第一行。
    • 请求的头部包含了关于客户端环境和请求的主体内容的有用信息。例如它可能包括浏览器设置的语言,主体内容的长度等等。每个头部通过一个回车换行符(CRLF)来分隔的。
    • 主体内容只是最下面一行

    1.2 HTTP 响应

    类似于 HTTP 请求,一个 HTTP 响应也包括三个组成部分:

    • 方法—统一资源标识符(URI)—协议/版本
    • 响应的头部
    • 主体内容

    下面是一个 HTTP 响应的例子:
    HTTP/1.1 200 OK
    Server: Microsoft-IIS/4.0
    Date: Mon, 5 Jan 2004 13:13:33 GMT
    Content-Type: text/html
    Last-Modified: Mon, 5 Jan 2004 13:13:12 GMT
    Content-Length: 112
    <html>
    <head>
    <title>HTTP Response Example</title>
    </head>
    <body>
    Welcome to Brainy Software
    </body>
    </html>

    说明:

    • 响应头部的第一行类似于请求头部的第一行。
    • 响应的主体内容是响应本身的 HTML 内容。

    ※ 我们不仅知道了http请求的格式,http响应的格式也知道了。而且聪明的你一定发现,用socket来发送http请求好像的确是可行的,只要按<big>约定</big>的格式发送请求的消息就可以了。

    2.用Socket发送/接收http请求

    我先写了一个简单的服务端程序,开放8088端口。
    以下的程序相当于,发送了这样一个http请求:
    GET/api/v1/games HTTP/1.1
    Host: localhost:8080
    Connection: Close

    public static void main(String[] args) throws ParseException, IOException, InterruptedException {
        Socket socket = new Socket("127.0.0.1", 8088);
        OutputStream os = socket.getOutputStream();
        boolean autoflush = true;
        PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        // send an HTTP request to the web server
        out.println("GET /api/v1/games HTTP/1.1");
        out.println("Host: localhost:8080");
        out.println("Connection: Close");
        out.println();
        // read the response
        boolean loop = true;
        StringBuffer sb = new StringBuffer(8096);
        while (loop) {
            if (in.ready()) {
                int i = 0;
                while (i != -1) {
                    i = in.read();
                    sb.append((char) i);
                }
                loop = false;
            }
            Thread.currentThread().sleep(50);
        }
        // display the response to the out console
        System.out.println(sb.toString());
        socket.close();
    }
    

    拓展阅读:
    HTTP Header 详解
    《How Tomcat Works》

    相关文章

      网友评论

        本文标题:[java] 理解http

        本文链接:https://www.haomeiwen.com/subject/gekdsttx.html