// Java 类文件
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class JavaHttpServer {
public static void main(String[] args) {
try {
HttpServer hs = HttpServer.create(new InetSocketAddress(8888), 0);// 设置HttpServer的端口为8888
hs.createContext(“/get/user”, new MyHandler());// 用MyHandler类内处理到
hs.setExecutor(null); // creates a default executor
hs.start();
} catch (Exception e) {
}
}
}
class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String line = null;
while((line = in.readLine()) != null){
System.out.println(line); // 是空的?为啥啊
}
String response = “come on baby”;
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
// 测试表单
提交
解决方案
40
HttpHandler HttpExchange 这两个类是本人写的吗?t.getRequestBody();这里面是怎么写的