首页>代码>java编写一个迷你版的Tomcat服务器,适合初学者>/MiniTomcat/src/com/tomlin/tomcat/handle/MyRequest.java
package com.tomlin.tomcat.handle;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: tomlin
 * @Description: 编写处理Request 请求处理代码
 * @ClassName: MyRequest
 * @Date: 2018/10/1 11:09
 * @Version: 1.0
 */
public class MyRequest {
    private String url;// 请求url
    private String method;//请求的方法

    public MyRequest(InputStream inputStream) throws IOException {
        String httpRequest = "";
        //一次读取多个字节
        byte[] httpRequestBytes = new byte[1024];
        int length = 0;
        if ((length = inputStream.read(httpRequestBytes)) > 0) {
            //读取到有效字符
            httpRequest = new String(httpRequestBytes, 0, length);
        }
        /*我们可以看到 HTTP请求头信息*/
        //GET / HTTP/1.1
        //Host: www.baidu.com
        //User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
        //Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
        //Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
        // Accept-Encoding: gzip, deflate

        //截取请求头
        String httpHead = httpRequest.split("\n")[0];
        url = httpHead.split("\\s")[1];
        method = httpHead.split("\\s")[0];
        System.out.println(this);
        System.out.println("请求方法 = " + method + "\n请求url=" + url);
    }

    public String getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }
}

最近下载更多
xun9er  LV1 2023年3月18日
lironggang  LV38 2021年6月16日
1316463958  LV8 2019年10月14日
ly3812  LV17 2019年10月2日
陆地火箭  LV3 2019年7月10日
ljkfdsjh  LV2 2019年4月10日
二两小面加个蛋  LV10 2019年3月6日
pdd  LV2 2019年2月14日
myzczx  LV25 2019年2月9日
xinjames  LV27 2019年2月5日
最近浏览更多
lee123321  LV22 2023年12月19日
annazhang  LV29 2023年10月3日
xun9er  LV1 2023年3月18日
al-aaalll  LV3 2022年12月13日
xsxtxbb  LV8 2022年10月10日
tomcat_test  LV2 2022年7月6日
isfrand  LV4 2022年4月18日
3188681084  LV1 2022年3月24日
李润石  LV2 2022年1月18日
lironggang  LV38 2021年6月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友