手写服务器httpserver_准备_Socket入门_htmlJAVA195-196

来源:http://www.bjsxt.com/
一、S02E195_01手写服务器httpserver_准备_Socket入门

package com.test.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 创建服务器,并启动
 */
public class Server {
    private ServerSocket server;

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
    /**
     * 启动方法
     */
    public void start(){        
        try {
            server = new ServerSocket(8888);
            this.receive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 接收客户端
     */
    private void receive(){
        try {
            Socket client =server.accept();
            StringBuilder sb =new StringBuilder();
            String msg =null;//接收客户端的请求信息

            BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

            while((msg=br.readLine()).length()>0){
                sb.append(msg);
                sb.append("\r\n");
                if(null==msg){
                    break;
                }
            }
            //接收客户端的请求信息
            String requestInfo =sb.toString().trim();       
            System.out.println(requestInfo);

        } catch (IOException e) {
            //e.printStackTrace();
        }
    }
    /**
     * 停止服务器
     */
    public void stop(){

    }
}

//输出信息:
GET / HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8

二、S02E196_01手写服务器httpserver_准备_html

1、get方式提交

<html>
    <head>
        <title>第一个表单</title>
    </head>
    <body>
        <pre>
            method:请求方式 get/post
                get:默认方式,数据量小,安全性不高
                post:量大,安全性相对高
            action:请求的服务器路径
            id:编号,前端(用户的浏览器)区分唯一性,js中使用
            name:名称,后端(服务器)区分唯一性,获取值
            只要提交数据给后台,必须存在name
        </pre>
        <form method="get" action="http://localhost:8888/index.html">
            用户名:<input type="text" name="uname" id="name">
            密码:<input type="password" name="pwd" id="pass">
            <input type="submit" value="登录">
        </form>
    </body>
</html>

用户请求get出去

//输出信息:(第一句包含uname=intputUname&pwd=inputPassword)
GET /index.html?uname=intputUname&pwd=inputPassword HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8

2、post方式提交
(method=”get” 改为method=”post”)

<html>
    <head>
        <title>第一个表单</title>
    </head>
    <body>
        <pre>
            method:请求方式 get/post
                get:默认方式,数据量小,安全性不高
                post:量大,安全性相对高
            action:请求的服务器路径
            id:编号,前端(用户的浏览器)区分唯一性,js中使用
            name:名称,后端(服务器)区分唯一性,获取值
            只要提交数据给后台,必须存在name
        </pre>
        <form method="post" action="http://localhost:8888/index.html">
            用户名:<input type="text" name="uname" id="name">
            密码:<input type="password" name="pwd" id="pass">
            <input type="submit" value="登录">
        </form>
    </body>
</html>

修改代码输出post方式的请求

package com.test.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 创建服务器,并启动
 */
public class ServerForPost {
    private ServerSocket server;

    public static void main(String[] args) {
        ServerForPost server = new ServerForPost();
        server.start();
    }
    /**
     * 启动方法
     */
    public void start(){        
        try {
            server = new ServerSocket(8888);
            this.receive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 接收客户端
     */
    private void receive(){
        try {
            Socket client =server.accept();
            StringBuilder sb =new StringBuilder();

            byte[] date = new byte[20480];
            int len = client.getInputStream().read(date);

            //接收客户端的请求信息
            String requestInfo = new String(date,0,len).trim();     
            System.out.println(requestInfo);

        } catch (IOException e) {
            //e.printStackTrace();
        }
    }
    /**
     * 停止服务器
     */
    public void stop(){

    }
}

//输出信息:(最后一句包含uname=intputUname&pwd=inputPassword)
POST /index.html HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Content-Length: 35
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8

uname=intputUname&pwd=inputPassword
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值