基于httpcorehttpclientcomponent搭建轻量级http服务器

上传人:876****10 文档编号:142026804 上传时间:2020-08-15 格式:DOC 页数:13 大小:152KB
返回 下载 相关 举报
基于httpcorehttpclientcomponent搭建轻量级http服务器_第1页
第1页 / 共13页
基于httpcorehttpclientcomponent搭建轻量级http服务器_第2页
第2页 / 共13页
基于httpcorehttpclientcomponent搭建轻量级http服务器_第3页
第3页 / 共13页
基于httpcorehttpclientcomponent搭建轻量级http服务器_第4页
第4页 / 共13页
基于httpcorehttpclientcomponent搭建轻量级http服务器_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《基于httpcorehttpclientcomponent搭建轻量级http服务器》由会员分享,可在线阅读,更多相关《基于httpcorehttpclientcomponent搭建轻量级http服务器(13页珍藏版)》请在金锄头文库上搜索。

1、基于httpcore(httpclient component)搭建轻量级http服务器 作者: 日期:基于httpcore(httpclient component)搭建轻量级http服务器下面是apache官网例子服务器端接受请求,实现接收文件请求处理器import java.io.File;import java.io.IOException;import java.io.InterruptedIOException;import .ServerSocket;import .Socket;import .URL;import .URLDecoder;import java.nio.cha

2、rset.Charset;import java.security.KeyStore;import java.util.Locale;import org.apache.http.ConnectionClosedException;import org.apache.http.HttpConnectionFactory;import org.apache.http.HttpEntity;import org.apache.http.HttpEntityEnclosingRequest;import org.apache.http.HttpException;import org.apache.

3、http.HttpRequest;import org.apache.http.HttpResponse;import org.apache.http.HttpServerConnection;import org.apache.http.HttpStatus;import org.apache.http.MethodNotSupportedException;import org.apache.http.entity.ContentType;import org.apache.http.entity.FileEntity;import org.apache.http.entity.Strin

4、gEntity;import org.apache.http.impl.DefaultBHttpServerConnection;import org.apache.http.impl.DefaultBHttpServerConnectionFactory;import org.apache.http.protocol.BasicHttpContext;import org.apache.http.protocol.HttpContext;import org.apache.http.protocol.HttpProcessor;import org.apache.http.protocol.

5、HttpProcessorBuilder;import org.apache.http.protocol.HttpRequestHandler;import org.apache.http.protocol.HttpService;import org.apache.http.protocol.ResponseConnControl;import org.apache.http.protocol.ResponseContent;import org.apache.http.protocol.ResponseDate;import org.apache.http.protocol.Respons

6、eServer;import org.apache.http.protocol.UriHttpRequestHandlerMapper;import org.apache.http.util.EntityUtils;import .ssl.KeyManager;import .ssl.KeyManagerFactory;import .ssl.SSLContext;import .ssl.SSLServerSocketFactory;/* * Basic, yet fully functional and spec compliant, HTTP/1.1 file server. */publ

7、ic class ElementalHttpServer public static void main(String args) throws Exception if (args.length = 2) port = Integer.parseInt(args1); / Set up the HTTP protocol processor HttpProcessor httpproc = HttpProcessorBuilder.create() .add(new ResponseDate() .add(new ResponseServer(Test/1.1) .add(new Respo

8、nseContent() .add(new ResponseConnControl().build(); / Set up request handlers UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper(); reqistry.register(*, new HttpFileHandler(docRoot); / Set up the HTTP service HttpService httpService = new HttpService(httpproc, reqistry); SSLServ

9、erSocketFactory sf = null; if (port = 8443) / Initialize SSL context ClassLoader cl = ElementalHttpServer.class.getClassLoader(); URL url = cl.getResource(my.keystore); if (url = null) System.out.println(Keystore not found); System.exit(1); KeyStore keystore = KeyStore.getInstance(jks); keystore.loa

10、d(url.openStream(), secret.toCharArray(); KeyManagerFactory kmfactory = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm(); kmfactory.init(keystore, secret.toCharArray(); KeyManager keymanagers = kmfactory.getKeyManagers(); SSLContext sslcontext = SSLContext.getInstance(TLS); ssl

11、context.init(keymanagers, null, null); sf = sslcontext.getServerSocketFactory(); Thread t = new RequestListenerThread(port, httpService, sf); t.setDaemon(false); t.start(); static class HttpFileHandler implements HttpRequestHandler private final String docRoot; public HttpFileHandler(final String do

12、cRoot) super(); this.docRoot = docRoot; public void handle( final HttpRequest request, final HttpResponse response, final HttpContext context) throws HttpException, IOException String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); if (!method.equals(GET) & !method.equals(HEAD) & !method.equals(POST) throw new MethodNotSupportedException(method + method not supported);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号