温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java webserver-获取请求协议和返回响应协议

发布时间:2020-08-04 07:10:19 来源:网络 阅读:218 作者:wx5d21d5e6e5ab1 栏目:编程语言

使用ServerSocket建立与浏览器的连接,获取请求协议

public class Server {
    private ServerSocket serverSocket;
    public static void main(String[]args)
    {
        Server server=new Server();
        server.start();
    }
    //启动服务
    public void start()
    {
        try {
            serverSocket=new ServerSocket(8888);
            receive();
        } catch (IOException e) {

        e.printStackTrace();
        System.out.println("服务器启动失败");
    }
}
//停止服务
public void stop()
{

}
//接受连接处理
public void receive()
{
    try {
        Socket client=serverSocket.accept();
        System.out.println("一个客户端建立了连接");
        //获取请求协议
        InputStream is =client.getInputStream();
        byte[] datas=new byte[1024*1024];
        int len=is.read(datas);  //读取完毕,并返回长度
        String requestInfo =new String(datas,0,len);
        System.out.println(requestInfo);

    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("客户端错误");
    }
}

}

返回响应协议:

public class Server02 {

private ServerSocket serverSocket;
public static void main(String[]args)
{
    Server02 server=new Server02();
    server.start();
}
//启动服务
public void start()
{
    try {
        serverSocket=new ServerSocket(8888);
        receive();
    } catch (IOException e) {

        e.printStackTrace();
        System.out.println("服务器启动失败");
    }
}
//停止服务
public void stop()
{

}
//接受连接处理
public void receive()
{
    try {
        Socket client=serverSocket.accept();
        System.out.println("一个客户端建立了连接");
        //获取请求协议
        InputStream is =client.getInputStream();
        byte[] datas=new byte[1024*1024];
        int len=is.read(datas);  //读取完毕,并返回长度
        String requestInfo =new String(datas,0,len);
        System.out.println(requestInfo);

        StringBuilder content =new StringBuilder();
        content.append("<html>");
        content.append("<head>");
        content.append("<title>");
        content.append("服务器响应成功");
        content.append("</title>");
        content.append("</head>");
        content.append("<body>");
        content.append("终于回来了");
        content.append("</body>");
        content.append("</html>");
        int size=content.toString().getBytes().length; //必须获取字节长度

        StringBuilder responseInfo =new StringBuilder();
        String blank =" ";
        String CRLF="\r\n";
        //拼接响应行
        responseInfo.append("HTTP/1.1").append(blank);
        responseInfo.append(200).append(blank);
        responseInfo.append("OK").append(CRLF);
        //返回
        //1、响应行:HTTP/1.1 200 OK
        //2、响应头(最后一行存在空行):
        /*
         Date:Mon,31Dec209904:25:57GMT
        Server:shsxt Server/0.0.1;charset=GBK    服务器内容
        Content-type:text/html                   内容类型
        Content-length:39725426                   内容长度
         */
        //拼接响应头
        responseInfo.append("Date:").append(new Date()).append(CRLF);
        responseInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
        responseInfo.append("Content-type:text/html").append(CRLF);
        responseInfo.append("Content-length:").append(size).append(CRLF);
        responseInfo.append(CRLF);   //响应头最后一行存在空行
        //3、 正文
        responseInfo.append(content.toString());

        //写出到客户端
        BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        bw.write(responseInfo.toString());
        bw.flush();

    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("客户端错误");
    }
}
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI