package com.wj.other; import com.sun.net.httpserver.HttpServer; import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; public class MockServer { public static void startServer() throws IOException { // 创建一个HTTP服务器,监听本地的9999端口 HttpServer server = HttpServer.create(new InetSocketAddress(9990), 0); // 设置根路径的处理器 server.createContext("/", httpExchange -> { // 假设所有POST请求都成功 if ("POST".equals(httpExchange.getRequestMethod())) { // 发送HTTP状态码200 httpExchange.sendResponseHeaders(200, 0); OutputStream os = httpExchange.getResponseBody(); os.write("Success".getBytes()); os.close(); } else { // 其他请求方法返回错误 httpExchange.sendResponseHeaders(405, 0); // 405 Method Not Allowed OutputStream os = httpExchange.getResponseBody(); os.write("Method Not Allowed".getBytes()); os.close(); } }); // 启动服务器 server.start(); System.out.println("Mock server started on port 9990"); } public static void main(String[] args) throws IOException { startServer(); } }