Module
jdk.httpserver
Package com.sun.net.httpserver
提供一个简单的高级Http服务器API,可用于构建嵌入式HTTP服务器。
支持“http”和“https”。
API提供了RFC 2616 (HTTP 1.1)和RFC 2818 (HTTP over TLS)的部分实现。
API提供的任何HTTP功能都可以通过使用API的应用程序代码实现。
程序员必须实现HttpHandler接口。 该接口提供了一个回调函数,用于处理来自客户端的传入请求。 HTTP请求及其响应称为交换。 HTTP交换由HttpExchange类表示。 HttpServer类用于侦听传入的TCP连接,并将这些连接上的请求调度到已经在服务器上注册的处理程序。
一个最小的Http服务器示例如下所示:
class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
read(is); // .. read the request body
String response = "This is the response";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
...
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/applications/myapp", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
上面的示例创建了一个简单的HttpServer,它使用调用应用程序线程调用针对到端口8000的传入http请求的handle()方法以及路径/ application / myapp /。
HttpExchange类封装了应用程序处理传入请求所需的所有内容,并生成适当的响应。
使用HttpServer注册处理程序会创建一个HttpContext对象,并且可以将Filter对象添加到返回的上下文中。 过滤器用于在交换机传递到交换处理程序之前执行自动的前处理和后处理。
对于敏感信息,可以使用HttpsServer来处理由SSL或TLS协议保护的“https”请求。 必须向HttpsServer提供一个HttpsConfigurator对象,其中包含初始化的SSLContext 。 HttpsConfigurator可用于配置密码套件和其他SSL操作参数。 可以创建一个简单的例子SSLContext如下:
char[] passphrase = "passphrase".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("testkeys"), passphrase);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, passphrase);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
在上面的示例中,使用keytool实用程序创建的名为“testkeys”的keystore文件用作客户端和服务器证书的证书存储。 以下代码显示了如何在HttpsConfigurator中使用SSLContext,以及SSLContext和HttpsConfigurator如何链接到HttpsServer。
server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
public void configure (HttpsParameters params) {
// get the remote address if needed
InetSocketAddress remote = params.getClientAddress();
SSLContext c = getSSLContext();
// get the default parameters
SSLParameters sslparams = c.getDefaultSSLParameters();
if (remote.equals (...) ) {
// modify the default set for client x
}
params.setSSLParameters(sslparams);
// statement above could throw IAE if any params invalid.
// eg. if app has a UI and parameters supplied by a user.
}
});
- 从以下版本开始:
- 1.6
-
接口摘要 接口 描述 HttpHandler 被调用以处理HTTP交换的处理程序。 -
类摘要 Class 描述 Authenticator 认证者表示HTTP认证机制的实现。Authenticator.Failure 表示认证失败。Authenticator.Result 来自authenticate()方法的返回类型的基类Authenticator.Retry 表示验证必须重试。Authenticator.Success 表示认证成功,通过调用getPrincipal()可以获取已认证的用户主体。BasicAuthenticator BasicAuthenticator提供HTTP Basic认证的实现。Filter 用于预处理和后处理传入请求的过滤器。Filter.Chain 与HttpServer关联的一连串过滤器。Headers HttpContext HttpContext表示应用程序的根URI路径与HttpHandler之间的映射,该对象被调用以处理在相关联的HttpServer或HttpsServer上发往该路径的请求。HttpExchange 此类封装了收到的HTTP请求和在一个交换中生成的响应。HttpPrincipal 表示通过HTTP Basic或Digest身份验证身份验证的用户。HttpsConfigurator 此类用于配置HttpsServer上每个传入https连接的https参数。HttpServer 这个类实现了一个简单的HTTP服务器。HttpsExchange 此类封装了收到的HTTPS请求和在一个交换中生成的响应,并定义了特定于HTTPS协议的HttpExchange扩展。HttpsParameters 表示与客户端协商的每个https连接的参数集。HttpsServer This class is an extension ofHttpServerwhich provides support for HTTPS.