java自己实现的httpserver

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方法的数据在后面

 

    <span style="font-family:'宋体';">你好</span><span style="font-family:Calibri;">

用户名:


密码:


登录">

 

 

 

 

 

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

你可能感兴趣的:(httpserver)