我无法创建 HTTPS 服务器

发布于 2025-01-16 13:08:28 字数 1054 浏览 2 评论 0原文

我正在尝试创建一个接受输入和输出 html 的 HTTPS 服务器,我已经创建了一个 HTTP 服务器,但是我现在试图了解 HTTPS 服务器代码是如何工作的。

import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;


public class SimpleHTTPServer {

    public static void main(String args[]) throws IOException {

        ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
        ServerSocket server = serverSocketFactory.createServerSocket(8080);

        System.out.println("Listening for connection on port 8080 ....");

        while (true) {
            try (Socket socket = server.accept()) {
                Date today = new Date();
                String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + today;
                socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
            }
        }
    }
}

此代码在 Firefox 上创建输出,但消息已加密。 如果有人能给我指出一个网站或视频,其中对如何使用 SSLSeverSocket 有很好的解释,我将非常感激,因为我找不到;或者如果有人可以在这里向我解释一下。

I am trying to create a HTTPS server that takes an input and outputs html, I have already made a HTTP server, however I am now trying to understand how HTTPS server code works.

import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;


public class SimpleHTTPServer {

    public static void main(String args[]) throws IOException {

        ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
        ServerSocket server = serverSocketFactory.createServerSocket(8080);

        System.out.println("Listening for connection on port 8080 ....");

        while (true) {
            try (Socket socket = server.accept()) {
                Date today = new Date();
                String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + today;
                socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
            }
        }
    }
}

This code creates an output, on firefox, but the message is encrypted.
I would really appreciate it if someone can point me to a website or a video that has a good explanation on how to use SSLSeverSocket since I cannot find one; or if someone could explain it to me here.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文