自分でHTTPプロトコルを実現する

8052 ワード

序文
最近HTTPプロトコルを研究していますので、自分で実現したいと思います.HTTPプロトコルはTCP/IPプロトコル族のアプリケーション層で働いています.トランスポート層で使うTCPの構造は大体このようです.
自己实现HTTP协议_第1张图片
新聞はこのようです.
自己实现HTTP协议_第2张图片
実現する
ここではSocketを使って実現し、ドメイン名解析の機能を追加しました.
package com.example;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MyHttpClient {

    public static class HTTPAddress {
        String hostAddress;
        int point = 80;

        public HTTPAddress(String hostAddress, int point) {
            this.hostAddress = hostAddress;
            this.point = point;
        }

    }

    public static void main(String[] args) {
        String url = "http://blog.csdn.net/qq_22706515/article/details/51819472";
        getStringByUrl(url);


    }

    public static final String HTTP_HEAD = "http://";

    public static HTTPAddress getHTTPAddress(String url) {
        if (url == null || url.equals("")) {
            return null;
        }
        String head = "";
        //    80
        int point = 80;
        if (url.startsWith(HTTP_HEAD)) {
            url = url.substring(HTTP_HEAD.length());
        }
        //      
        head = url.split("/")[0];
        //   url     ,     
        int index = head.indexOf(":");
        if (index != -1) {
            String pointStr = head.substring(index + 1).trim();
            head = head.substring(0, index);
            point = Integer.valueOf(pointStr);
        }
        try {
            //      ip  
            String hostAddress = InetAddress.getByName(head).getHostAddress();
            HTTPAddress httpAddress = new HTTPAddress(hostAddress, point);
            return httpAddress;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void getStringByUrl(String url) {
        try {
            HTTPAddress httpAddress = getHTTPAddress(url);
            if (httpAddress == null) {
                return;
            }
            Socket socket = new Socket(httpAddress.hostAddress, httpAddress.point);
            OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
            StringBuffer sb = new StringBuffer();
            //     ,       \r
sb.append("GET " + url + " HTTP/1.1\r
"
); sb.append("Host: " + httpAddress.hostAddress + ":" + httpAddress.point + "\r
"
); sb.append("Connection: keep-alive\r
"
); // \r
sb.append("\r
"
); osw.write(sb.toString()); osw.flush(); InputStream is = socket.getInputStream(); int count = 0; byte[] b = new byte[8192]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = is.read(b)) != -1) { baos.write(b, 0, count); } is.close(); baos.close(); String result = baos.toString(); // , , String separateStr="\r
\r
"
; int index = result.indexOf(separateStr); String headStr = result.substring(0, index); String contentStr=result.substring(index+separateStr.length()); System.out.print("head:"+headStr+"
"
); System.out.print("content:"+contentStr+"
"
); } catch (Exception e) { e.printStackTrace(); } } }
テスト
これは戻ってきた内容です.自分の必要に応じて部分を取得することができます.
自己实现HTTP协议_第3张图片