HttpServer是JDK1.6以后內(nèi)置的HTTP服務(wù)器,位置在rt.jar的com.sun.net.httpserver包下。
使用HttpServer實現(xiàn)HTTP服務(wù)器主要涉及下面幾個類:
1.HttpServer:表示一個服務(wù)器實例,需要綁定一個IP地址和端口號。(HttpsServer是其子類,處理https請求)
2.HttpContext:服務(wù)器監(jiān)聽器的上下文,需要配置用于匹配URI的公共路徑和用來處理請求的HttpHandler
(可以創(chuàng)建多個 HttpContext,一個 HttpContext 對應(yīng)一個 HttpHandler,不同的 URI 請求,根據(jù)添加的 HttpContext 監(jiān)聽器,分配到對應(yīng)的 HttpHandler 處理請求)
3.HttpHandler:上下文對應(yīng)的http請求處理器
4.HttpExchange:監(jiān)聽器回調(diào)時傳入的參數(shù),封裝了http請求和響應(yīng)的所有數(shù)據(jù)操作
在使用eclipse創(chuàng)建項目時,如果添加httpserver的包時顯示找不到httpserver包是因為sun.net包里的類eclipse默認禁止使用。
解決方法:
工程上右鍵->工程屬性->java builder path->Libraries標(biāo)簽,點擊JRE System Library里面的Access rules,添加 com/sun/net/httpserver/*** 為accessible,如果該項存在,就edit
Demo代碼:
package httpservice;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
public class MyHttpService {
//啟動后訪問:http://localhost:8888/test
public static void main(String[] args) throws Exception {
*//創(chuàng)建http服務(wù)器,綁定本地8888端口*
HttpServer httpServer = HttpServer.create(new InetSocketAddress(8888), 0);
*//創(chuàng)建上下文監(jiān)聽,攔截包含/test的請求*
httpServer.createContext("/test", new TestHttpHandler());
httpServer.start();
}
}
package httpservice;
import java.io.IOException;
import java.io.OutputStream;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
public class TestHttpHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
String response = "test message";
exchange.sendResponseHeaders(200, 0);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes("UTF-8"));
os.close();
}
}
1.HttpServer 常用方法:
// 重新綁定地址和端口
void bind?(InetSocketAddress addr, int backlog)
// 獲取當(dāng)前綁定的地址
InetSocketAddress getAddress?()
/**
* 創(chuàng)建監(jiān)聽的上下文, 請求 URI 根路徑的匹配, 根據(jù)不同的 URI 根路徑選擇不同的 HttpHandler 處理請求,
* 路徑必須以 "/" 開頭。路徑 "/" 表示匹配所有的請求 URI(沒有其他更具體的匹配路徑除外)。
*/
HttpContext createContext?(String path)
HttpContext createContext?(String path, HttpHandler handler)
// 移除上下文監(jiān)聽
void removeContext?(HttpContext context)
void removeContext?(String path)
// 設(shè)置請求的線程執(zhí)行器, 設(shè)置為 null 表示使用默認的執(zhí)行器
void setExecutor?(Executor executor)
Executor getExecutor?()
// 啟動服務(wù)
void start?()
// 最長等待指定時間后停止服務(wù)
void stop?(int delay)
2.HttpContext
HttpServer httpServer = HttpServer.create(...);
/*
* 上下文監(jiān)聽器對應(yīng)的 URI 根路徑,必須以 "/" 開頭,
* 表示以 "/xxx" 開頭的 URI 請求都交給對應(yīng)的 httpHandler 處理,
* "/" 表示匹配所有的請求, 一個請求只會交給 path 最匹配的一個上下文去處理(不能重復(fù)處理)
*/
String path = "/xxx";
// 可以創(chuàng)建多個,以實現(xiàn)更細致的 URI 路徑匹配來分開處理來自不同 URI 路徑的請求
httpServer.createContext(path, new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
// 處理匹配當(dāng)前上下文 path 的請求
}
});
3.HttpHandler ( HttpExchange )
// 獲取請求的 URI, 請求鏈接除去協(xié)議和域名端口后的部分, 如: http://www.abc.com/aa/bb, URI 為 /aa/bb
URI getRequestURI?()
// 獲取請求客戶端的 IP 地址
InetSocketAddress getRemoteAddress?()
// 獲取請求協(xié)議, 例如: HTTP/1.1
String getProtocol?()
// 獲取請求的方法, "GET", "POST" 等
String getRequestMethod?()
// 獲取所有的請求頭
Headers getRequestHeaders?()
// 以輸入流的方式獲取請求內(nèi)容
InputStream getRequestBody?()
// 獲取響應(yīng)頭的 Map, 要添加頭, 獲取到 headers 后調(diào)用 add(key, value) 方法添加
Headers getResponseHeaders?()
// 發(fā)送響應(yīng)頭, 并指定 響應(yīng)code 和 響應(yīng)內(nèi)容的長度
void sendResponseHeaders?(int rCode, long responseLength)
// 獲取響應(yīng)內(nèi)容的輸出流, 響應(yīng)內(nèi)容寫到該流
OutputStream getResponseBody?()
// 關(guān)閉處理器, 同時將關(guān)閉請求和響應(yīng)的輸入輸出流(如果還沒關(guān)閉)
void close?()
// 獲取此請求對應(yīng)的上下文對象
HttpContext getHttpContext?()
// 獲取收到請求的本地地址
InetSocketAddress getLocalAddress?()