如何在没有 Spring、Java EE 等 Web 框架的情况下使用 Java 创建 HTTP API?

发布于 2025-01-10 17:34:32 字数 1437 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹淡然 2025-01-17 17:34:32

HTTP 的基础知识相当简单。打开 ServerSocket 来侦听传入请求。建立连接后,启动一个新线程并发送响应。这可能看起来像,

public static void main(String[] args) {
    try {
        ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(8080, 10);
        StringBuilder body = new StringBuilder();
        body.append("<html><body><h1>Hello, World!</h1></body></html>");
        while (true) {
            Socket s = ss.accept();
            Thread t = new Thread(new HttpReply(s, body));
            t.start();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

然后要实际发送响应,您从 Socket 获取 OutputStream 并写入所需的 HTTP 标头,然后写入正文。就像,

class HttpReply implements Runnable {
    private Socket s;
    private StringBuilder body;

    private HttpReply(Socket s, StringBuilder body) {
        this.s = s;
        this.body = body;
    }

    public void run() {
        try {
            PrintStream ps = new PrintStream(s.getOutputStream());
            ps.println("HTTP/1.1 200 OK");
            ps.println("Date: Mon, 27 Jul 2009 12:28:53 GMT");
            ps.println("Server: Java");
            ps.println("Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT");
            ps.println("Content-Length: " + body.length());
            ps.println("Content-Type: text/html");
            ps.println("Connection: Closed");
            ps.println();
            ps.println(body);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

它将在您的计算机的端口 8080 上侦听请求并使用基本的 hello world 网页进行回复。

Hello World Web页面

The basics of HTTP are fairly simple. Open a ServerSocket to listen for incoming requests. When a connection is made, start a new thread and send the response. That could look like,

public static void main(String[] args) {
    try {
        ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(8080, 10);
        StringBuilder body = new StringBuilder();
        body.append("<html><body><h1>Hello, World!</h1></body></html>");
        while (true) {
            Socket s = ss.accept();
            Thread t = new Thread(new HttpReply(s, body));
            t.start();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Then to actually send the response you get an OutputStream from the Socket and write the required HTTP headers and then the body. Like,

class HttpReply implements Runnable {
    private Socket s;
    private StringBuilder body;

    private HttpReply(Socket s, StringBuilder body) {
        this.s = s;
        this.body = body;
    }

    public void run() {
        try {
            PrintStream ps = new PrintStream(s.getOutputStream());
            ps.println("HTTP/1.1 200 OK");
            ps.println("Date: Mon, 27 Jul 2009 12:28:53 GMT");
            ps.println("Server: Java");
            ps.println("Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT");
            ps.println("Content-Length: " + body.length());
            ps.println("Content-Type: text/html");
            ps.println("Connection: Closed");
            ps.println();
            ps.println(body);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Which will listen on port 8080 of your machine for requests and reply with a basic hello world web page.

Hello World Web Page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文