java自己实现的httpserver

来源:互联网 发布:sql 相同id 合并字段 编辑:程序博客网 时间:2024/06/05 14:28

package my.httpserver;

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.BindException;

import java.net.ServerSocket;

import java.net.Socket;

 

public class Server2 {

private ServerSocket server;

public static void main(String[] args) {

new Server2().start();

}

public void start() {

try {

server = new ServerSocket(8888);

receive();

} catch (BindException e) {

System.out.println("端口已被占用");

} catch(IOException e) {

e.printStackTrace();

}

}

public void receive() {

try {

Socket client = server.accept();

byte[] data = new byte[20480];

int len = client.getInputStream().read(data);

String msg = new String(data, 0, len);

System.out.println(msg);

} catch(IOException e) {

e.printStackTrace();

}

}

}

 

 

 注意post方法的数据在后面

 

<html>

    <head><title>你好</title></head>

<body>

<form method = "get" action = "http://localhost:8888/">

用户名:<input type = "text" name = "username" id = "username">

<br>密码:<input type = "password" name = "pwd" id = "pwd">

<br><input type = "submit" value = "登录">

</form>

</body>

 

</html>

 

 

 

 

GET /?username=2123&pwd=nihao HTTP/1.1

Host: localhost:8888

Connection: keep-alive

Upgrade-Insecure-Requests: 1

User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

Accept-Encoding: gzip, deflate, sdch, br

Accept-Language: zh-CN,zh;q=0.8

 

 

 

 

POST / HTTP/1.1

Host: localhost:8888

Connection: keep-alive

Content-Length: 31

Cache-Control: max-age=0

Origin: null

Upgrade-Insecure-Requests: 1

User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

Content-Type: application/x-www-form-urlencoded

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

Accept-Encoding: gzip, deflate, br

Accept-Language: zh-CN,zh;q=0.8

 

username=dazzling&pwd=haojunjie

原创粉丝点击