亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

HttpServer和HttpsServer簡(jiǎn)單實(shí)現(xiàn)

系統(tǒng) 2900 0
javaSE本身自己就實(shí)現(xiàn)了web功能,分別有兩種實(shí)現(xiàn)一種是HTTP、HTTPS,實(shí)現(xiàn)類分別為HttpServer和HttpsServer但是都要自己實(shí)現(xiàn)HttpHandler,而且HTTPS的實(shí)現(xiàn)要麻煩一些需要?jiǎng)?chuàng)建證書(shū)。創(chuàng)建證書(shū)的工具java的bin目錄下有名字叫keytool創(chuàng)建方法入下:
HttpServer和HttpsServer簡(jiǎn)單實(shí)現(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實(shí)現(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實(shí)現(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");   //建立證書(shū)庫(kù)    
		ks.load(new FileInputStream("F:/serverkeys"), "luoxun".toCharArray());  //載入證書(shū)    
		KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");  //建立一個(gè)密鑰管理工廠    
		kmf.init(ks, "luoxun".toCharArray());  //初始工廠    
		SSLContext sslContext = SSLContext.getInstance("SSLv3");  //建立證書(shū)實(shí)體    
		sslContext.init(kmf.getKeyManagers(), null, null);   //初始化證書(shū)   
		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>主機(jī)名::%s</p>");
			sb.append("<p>端口號(hào):%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簡(jiǎn)單實(shí)現(xiàn)


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 人人看操 | 99re6这里只有精品视频 | 日本在线观看不卡 | 精品国产日韩一区三区 | 午夜精品九九九九99蜜桃 | 欧美亚洲国产精品久久久久 | 国产精品久久久久久吹潮 | 亚洲日本va中文字幕婷婷 | 久久看免费视频 | 国产成人免费高清在线观看 | 国产中文字幕亚洲 | 在线观看亚洲免费视频 | 婷婷综合另类小说色区 | 免费中文字幕不卡视频 | 国产成人免费观看在线视频 | 激情五月婷婷久久 | 四虎资源| 国产好大好爽久久久久久久 | 黄色网址在线免费 | 欧美精品h在线播放 | 成人a毛片高清视频 | 99成人精品| 美女福利视频一区二区 | 亚洲免费久久 | 亚洲免费色视频 | 日本老熟妇激情毛片 | 免费网站啪啪大全 | 欧美在线观看一区二区 | 久草在线资源网 | 亚洲国产视频在线 | 国产性一交一乱一伦一色一情 | 午夜综合 | 在线播放色 | 一区二区三区久久精品 | 国产乱叫456在线 | 亚洲第一视频在线播放 | 日韩手机看片 | 亚洲大胆精品337p色 | 国产二区视频 | 四虎成人免费视频 | 波多野结衣一区二区在线 |