/**<pre>
Provides a simple high-level Http server API, which can be used to build embedded HTTP servers.
Both "http" and "https" are supported.
The API provides a partial implementation of RFC 2616 (HTTP 1.1) and RFC 2818 (HTTP over TLS).
Any HTTP functionality not provided by this API can be implemented by application code
using the API.
Programmers must implement the HttpHandler interface.
This interface provides a callback which is invoked to handle incoming requests from clients.
A HTTP request and its response is known as an exchange.
The HttpServer class is used to listen for incoming TCP connections and it dispatches requests
on these connections to handlers which have been registered with the server.
For sensitive information, a HttpsServer can be used to process "https" requests secured by the SSL or
TLS protocols. A HttpsServer must be provided with a HttpsConfigurator object,
which contains an initialized SSLContext. HttpsConfigurator can be used to configure the cipher suites and
other SSL operating parameters. A simple example SSLContext could be created as follows:
char[] passphrase = "passphrase".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("testkeys"), passphrase);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, passphrase);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
The following code shows how the SSLContext is then used in a HttpsConfigurator and how the SSLContext
and HttpsConfigurator are linked to the HttpsServer.
server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
public void configure (HttpsParameters params) {
// get the remote address if needed
InetSocketAddress remote = params.getClientAddress();
SSLContext c = getSSLContext();
// get the default parameters
SSLParameters sslparams = c.getDefaultSSLParameters();
if (remote.equals (...) ) {
// modify the default set for client x
}
params.setSSLParameters(sslparams);
// statement above could throw IAE if any params invalid.
// eg. if app has a UI and parameters supplied by a user.
}
});
</pre> */
public class HttpServerDemo {
public static void main(String[] args) throws IOException {
startHttpServer();
}
static void startHttpServer() throws IOException {
int port = 8888;
InetSocketAddress addr = new InetSocketAddress(port);
HttpServer server = HttpServer.create(addr, 0);
server.createContext("/myApp", new MyHandler());
//server.setExecutor(null); // creates a default executor
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server is listening on port " + port + "...");
}
}
class MyHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String requestMethod = exchange.getRequestMethod();
System.out.println("RequestMethod: " + requestMethod);
if (requestMethod.equalsIgnoreCase("GET")) {
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/html"); //text/plain
exchange.sendResponseHeaders(200, 0);
OutputStream responseBody = exchange.getResponseBody();
Headers requestHeaders = exchange.getRequestHeaders();
Set<String> keySet = requestHeaders.keySet();
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()) {
String key = iter.next();
List<String> values = requestHeaders.get(key);
String s = "<b>" + key + "</b> = " + values.toString() + "<br>";
responseBody.write(s.getBytes());
}
responseBody.close();
}
}
}
分享到:
相关推荐
一个HTTP 服务器的源码... 简单易懂 共享给大家
java HttpServer源码工程
详细的演示了httpserver工作过程的源码。
//关键字:Delphi XE8、HTTPSERVER、application/json //服务程序运行了几个月,基本正常,偶尔会出现异步操作异常(因所有连接都是共享同一个ado连接的原因) //以下部分关键代码供参考; TMainForm = class(TForm)...
收到一个 HTTP 请求时(其实就是 listen 的端口 accpet 的时候),派生一个线程运行 accept_request 函数。 取出 HTTP 请求中的 method (GET 或 POST) 和 url。对于 GET 方法,如果有携带参数,则 query_string ...
HttpServer C# 通过winform启动http服务器 HttpServer C# 通过winform启动http服务器
./httpserver ~~~ **客户端**:一方面可以通过浏览器直接进行服务器访问,一方面可以使用自己创建的客户端进行连接和消息互传(使用方案如下): ~~~c g++ simclient.cpp ./a.out ip port ~~~ ![](./image/out.jpg)...
C#版支持高并发的HTTP服务器源码,异步处理并发调用,应用于WINFORM程序中,创建自己的HTTP SERVER的首选办法。
libuv-httpserver-源码.rar
c# socket编程实例一 FTPserver源码,非常经典的源码。或者访问http://www.tangshansoft.cn
采用stm32f407+w5500模块,实现spi转以太网发送,实测可用.采用TIM2定时1ms发送一次数据
1.全网唯一的Java 实现根据http请求协议源码的独立HttpServer 服务,无需首先web、webservice 等问题,自由注册。自由访问; 2.灵活的可配置的xml 文件 3.使用只需要一个jar 包 和一个xml 引入即可,支持多端口、多...
http服务器Java HTTP Server 实现,适合您的风格
关于Qt HttpServer的一些测试(Qt6.4.0rc)测试源码 https://blog.csdn.net/aggs1990/article/details/126995555 CSDN审核可能较慢,如无法下载,可以过段时间再回来看下 仅供相关爱好者交流使用,请于下载24小时内...
一套优秀的基于C#开发的HTTP web server类库源码及例子程序
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为...基于Reactor模型+线程池的httpserver(源码+项目说明).zip
此源码有问题,请下载downmoon提供的修正版!非常抱歉! http://download.csdn.net/source/2434932
服务器监听来自idHttp的数据,运行两个实例中的程序,可以相互发送文字信息或文件了,
c++ http通讯 基于mongoose 的httpserver和httpclient。Mongoose是一个轻量化的web服务器,通过提供一个web接口给它,它可以嵌入到现有的工程当中。Mongoose的整个代码只有一个c文件和一个h文件,使用起来非常方便。...