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,它使用调用应用程序线程来调用handle()方法,用于传入指向端口8000的http请求,以及路径/ applications / 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”的密钥库文件用作客户端和服务器证书的证书存储。 下面的代码显示了如何在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交换的处理程序。 -  
      
类摘要 类 描述 Authenticator Authenticator表示HTTP身份验证机制的实现。Authenticator.Failure 表示身份验证失败。Authenticator.Result 来自authenticate()方法的返回类型的基类Authenticator.Retry 表示必须重试身份验证。Authenticator.Success 表示身份验证成功,可以通过调用getPrincipal()获取经过身份验证的用户主体。BasicAuthenticator BasicAuthenticator提供HTTP基本身份验证的实现。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.