欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

HttpServer和HttpsServer簡單實現(xiàn)

系統(tǒng) 2707 0
javaSE本身自己就實現(xiàn)了web功能,分別有兩種實現(xiàn)一種是HTTP、HTTPS,實現(xiàn)類分別為HttpServer和HttpsServer但是都要自己實現(xiàn)HttpHandler,而且HTTPS的實現(xiàn)要麻煩一些需要創(chuàng)建證書。創(chuàng)建證書的工具java的bin目錄下有名字叫keytool創(chuàng)建方法入下:
HttpServer和HttpsServer簡單實現(xiàn)
    import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;


public class HTTpServer {
	public static void main(String[] args) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException {
		//http實現(xiàn)
		HttpServer http = HttpServer.create(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 8989),0);
		http.createContext("/web", new HTTpServer().new MyHandler());
		http.setExecutor(null);
		http.start();
		//https實現(xiàn)
		HttpsServer https = HttpsServer.create(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 8787), 0);
		https.createContext("/web", new HTTpServer().new MyHandler());
		https.setExecutor(null);
		KeyStore ks = KeyStore.getInstance("JKS");   //建立證書庫    
		ks.load(new FileInputStream("F:/serverkeys"), "luoxun".toCharArray());  //載入證書    
		KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");  //建立一個密鑰管理工廠    
		kmf.init(ks, "luoxun".toCharArray());  //初始工廠    
		SSLContext sslContext = SSLContext.getInstance("SSLv3");  //建立證書實體    
		sslContext.init(kmf.getKeyManagers(), null, null);   //初始化證書   
		HttpsConfigurator httpsConfigurator = new HttpsConfigurator(sslContext);
		https.setHttpsConfigurator(httpsConfigurator);
		https.start();
	}
	class MyHandler implements HttpHandler{
		public void handle(HttpExchange exchange) throws IOException {
			String protocol = exchange.getProtocol();
			String method    = exchange.getRequestMethod();
			String url = exchange.getRequestURI().toString();
			String query     = exchange.getRequestURI().getQuery();
			
			InputStream  request  =exchange.getRequestBody();
			OutputStream response = exchange.getResponseBody();
			
			InetSocketAddress address = exchange.getRemoteAddress();
			String host = address.getHostName();
			String port = String.valueOf(address.getPort());
			
			StringBuilder sb = new StringBuilder();
			sb.append("<meta http-equiv='charset' content='text/html;charset=gb2312'>");
			sb.append("<p>協(xié)議:%s</p>");
			sb.append("<p>提交方式:%s</p>");
			sb.append("<p>URL:%s</p>");
			sb.append("<p>參數(shù)列表:%s</p>");
			sb.append("<p>主機名::%s</p>");
			sb.append("<p>端口號:%s</p>");
			String content = String.format(sb.toString(), protocol,method,url,query,host,port);
			
			byte[] contentBin = content.getBytes();
			exchange.sendResponseHeaders(200, contentBin.length);
			response.write(contentBin);
			response.flush();
			response.close();
		}
	}
}

  

HttpServer和HttpsServer簡單實現(xiàn)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久草成人在线 | 伊人亚洲精品 | 久久久91精品国产一区二区三区 | 美国黄色一级片 | 亚洲欧美日韩高清 | 久久久久久久久日本理论电影 | 午夜草逼 | 亚洲第一成人影院 | 成人av观看 | 亚洲国产咪爱网 | 亚洲色图国产 | 日夜夜操| 亚洲一区二区中文字幕 | 99久久精品费精品国产一区二 | 国产精品久久久久久久久久久久 | 在线视频a | av国语| 国产日韩欧美中文字幕 | 国产精品无码人妻系列AV | 亚洲人成网站999久久久综合 | 成人福利在线视频免费观看 | 91蝌蚪九色| 国产小视频在线播放 | 麻豆av电影在线观看 | 在线观看日韩 | 欧美综合国产精品久久丁香 | 天天干干干 | 久久乐国产精品 | 午夜小视频在线播放 | 激情插插插 | 欧美高清性色生活片免费观看 | 国产成人影院 | 亚洲黑人在线观看 | 精品久久香蕉国产线看观看亚洲 | 欧美ol丝袜高跟秘书在线播放 | 国内久久 | 色拍拍在精品视频69影院在线 | 欧美亚洲理伦电影毛片在线播放 | 欧美一区二区三区久久久 | 亚洲精品久久久一区二区三区 | 奇米影视图片 |