HttpServer和HttpsServer简单实现

HttpServer和HttpsServer简单实现

博客分类:
 
javaSE本身自己就实现了web功能,分别有两种实现一种是HTTP、HTTPS,实现类分别为HttpServer和HttpsServer但是都要自己实现HttpHandler,而且HTTPS的实现要麻烦一些需要创建证书。创建证书的工具java的bin目录下有名字叫keytool创建方法入下:

Java代码  收藏代码
  1. import java.io.FileInputStream;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.OutputStream;  
  5. import java.net.InetAddress;  
  6. import java.net.InetSocketAddress;  
  7. import java.security.KeyManagementException;  
  8. import java.security.KeyStore;  
  9. import java.security.KeyStoreException;  
  10. import java.security.NoSuchAlgorithmException;  
  11. import java.security.UnrecoverableKeyException;  
  12. import java.security.cert.CertificateException;  
  13.   
  14. import javax.net.ssl.KeyManagerFactory;  
  15. import javax.net.ssl.SSLContext;  
  16.   
  17. import com.sun.net.httpserver.HttpExchange;  
  18. import com.sun.net.httpserver.HttpHandler;  
  19. import com.sun.net.httpserver.HttpServer;  
  20. import com.sun.net.httpserver.HttpsConfigurator;  
  21. import com.sun.net.httpserver.HttpsServer;  
  22.   
  23.   
  24. public class HTTpServer {  
  25.     public static void main(String[] args) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException {  
  26.         //http实现  
  27.         HttpServer http = HttpServer.create(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 8989),0);  
  28.         http.createContext("/web", new HTTpServer().new MyHandler());  
  29.         http.setExecutor(null);  
  30.         http.start();  
  31.         //https实现  
  32.         HttpsServer https = HttpsServer.create(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 8787), 0);  
  33.         https.createContext("/web", new HTTpServer().new MyHandler());  
  34.         https.setExecutor(null);  
  35.         KeyStore ks = KeyStore.getInstance("JKS");   //建立证书库      
  36.         ks.load(new FileInputStream("F:/serverkeys"), "luoxun".toCharArray());  //载入证书      
  37.         KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");  //建立一个密钥管理工厂      
  38.         kmf.init(ks, "luoxun".toCharArray());  //初始工厂      
  39.         SSLContext sslContext = SSLContext.getInstance("SSLv3");  //建立证书实体      
  40.         sslContext.init(kmf.getKeyManagers(), null, null);   //初始化证书     
  41.         HttpsConfigurator httpsConfigurator = new HttpsConfigurator(sslContext);  
  42.         https.setHttpsConfigurator(httpsConfigurator);  
  43.         https.start();  
  44.     }  
  45.     class MyHandler implements HttpHandler{  
  46.         public void handle(HttpExchange exchange) throws IOException {  
  47.             String protocol = exchange.getProtocol();  
  48.             String method    = exchange.getRequestMethod();  
  49.             String url = exchange.getRequestURI().toString();  
  50.             String query     = exchange.getRequestURI().getQuery();  
  51.               
  52.             InputStream  request  =exchange.getRequestBody();  
  53.             OutputStream response = exchange.getResponseBody();  
  54.               
  55.             InetSocketAddress address = exchange.getRemoteAddress();  
  56.             String host = address.getHostName();  
  57.             String port = String.valueOf(address.getPort());  
  58.               
  59.             StringBuilder sb = new StringBuilder();  
  60.             sb.append("<meta http-equiv='charset' content='text/html;charset=gb2312'>");  
  61.             sb.append("<p>协议:%s</p>");  
  62.             sb.append("<p>提交方式:%s</p>");  
  63.             sb.append("<p>URL:%s</p>");  
  64.             sb.append("<p>参数列表:%s</p>");  
  65.             sb.append("<p>主机名::%s</p>");  
  66.             sb.append("<p>端口号:%s</p>");  
  67.             String content = String.format(sb.toString(), protocol,method,url,query,host,port);  
  68.               
  69.             byte[] contentBin = content.getBytes();  
  70.             exchange.sendResponseHeaders(200, contentBin.length);  
  71.             response.write(contentBin);  
  72.             response.flush();  
  73.             response.close();  
  74.         }  
  75.     }  
  76. }  
posted @ 2020-06-13 22:14  吃饭了吗  阅读(3253)  评论(0编辑  收藏  举报