HttpServer和HttpsServer简单实现

javaSE本身自己就实现了web功能,分别有两种实现一种是HTTP、HTTPS,实现类分别为HttpServer和HttpsServer但是都要自己实现HttpHandler,而且HTTPS的实现要麻烦一些需要创建证书。创建证书的工具java的bin目录下有名字叫keytool创建方法入下:
[img]http://dl.iteye.com/upload/attachment/0067/2031/4a99a809-361e-3217-90e3-3e63b9695bcb.jpg[/img]
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实现
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实现
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>协议:%s</p>");
sb.append("<p>提交方式:%s</p>");
sb.append("<p>URL:%s</p>");
sb.append("<p>参数列表:%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();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值