HttpServer是JDK1.6以后內(nèi)置的HTTP服務(wù)器,位置在rt.jar的com.sun.net.httpserver包下。
使用HttpServer實(shí)現(xiàn)HTTP服務(wù)器主要涉及下面幾個(gè)類:
1.HttpServer:表示一個(gè)服務(wù)器實(shí)例玩祟,需要綁定一個(gè)IP地址和端口號(hào)。(HttpsServer是其子類,處理https請(qǐng)求)
2.HttpContext:服務(wù)器監(jiān)聽(tīng)器的上下文搓彻,需要配置用于匹配URI的公共路徑和用來(lái)處理請(qǐng)求的HttpHandler
(可以創(chuàng)建多個(gè) HttpContext,一個(gè) HttpContext 對(duì)應(yīng)一個(gè) HttpHandler嘱朽,不同的 URI 請(qǐng)求旭贬,根據(jù)添加的 HttpContext 監(jiān)聽(tīng)器,分配到對(duì)應(yīng)的 HttpHandler 處理請(qǐng)求)
3.HttpHandler:上下文對(duì)應(yīng)的http請(qǐng)求處理器
4.HttpExchange:監(jiān)聽(tīng)器回調(diào)時(shí)傳入的參數(shù)搪泳,封裝了http請(qǐng)求和響應(yīng)的所有數(shù)據(jù)操作
在使用eclipse創(chuàng)建項(xiàng)目時(shí)稀轨,如果添加httpserver的包時(shí)顯示找不到httpserver包是因?yàn)閟un.net包里的類eclipse默認(rèn)禁止使用。
解決方法:
工程上右鍵->工程屬性->java builder path->Libraries標(biāo)簽岸军,點(diǎn)擊JRE System Library里面的Access rules奋刽,添加 com/sun/net/httpserver/*** 為accessible瓦侮,如果該項(xiàng)存在,就edit
Demo代碼:
package httpservice;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
public class MyHttpService {
//啟動(dòng)后訪問(wèn):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)聽(tīng),攔截包含/test的請(qǐng)求*
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)聽(tīng)的上下文, 請(qǐng)求 URI 根路徑的匹配, 根據(jù)不同的 URI 根路徑選擇不同的 HttpHandler 處理請(qǐng)求,
* 路徑必須以 "/" 開(kāi)頭肚吏。路徑 "/" 表示匹配所有的請(qǐng)求 URI(沒(méi)有其他更具體的匹配路徑除外)。
*/
HttpContext createContext?(String path)
HttpContext createContext?(String path, HttpHandler handler)
// 移除上下文監(jiān)聽(tīng)
void removeContext?(HttpContext context)
void removeContext?(String path)
// 設(shè)置請(qǐng)求的線程執(zhí)行器, 設(shè)置為 null 表示使用默認(rèn)的執(zhí)行器
void setExecutor?(Executor executor)
Executor getExecutor?()
// 啟動(dòng)服務(wù)
void start?()
// 最長(zhǎng)等待指定時(shí)間后停止服務(wù)
void stop?(int delay)
2.HttpContext
HttpServer httpServer = HttpServer.create(...);
/*
* 上下文監(jiān)聽(tīng)器對(duì)應(yīng)的 URI 根路徑狭魂,必須以 "/" 開(kāi)頭,
* 表示以 "/xxx" 開(kāi)頭的 URI 請(qǐng)求都交給對(duì)應(yīng)的 httpHandler 處理,
* "/" 表示匹配所有的請(qǐng)求, 一個(gè)請(qǐng)求只會(huì)交給 path 最匹配的一個(gè)上下文去處理(不能重復(fù)處理)
*/
String path = "/xxx";
// 可以創(chuàng)建多個(gè)罚攀,以實(shí)現(xiàn)更細(xì)致的 URI 路徑匹配來(lái)分開(kāi)處理來(lái)自不同 URI 路徑的請(qǐng)求
httpServer.createContext(path, new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
// 處理匹配當(dāng)前上下文 path 的請(qǐng)求
}
});
3.HttpHandler ( HttpExchange )
// 獲取請(qǐng)求的 URI, 請(qǐng)求鏈接除去協(xié)議和域名端口后的部分, 如: http://www.abc.com/aa/bb, URI 為 /aa/bb
URI getRequestURI?()
// 獲取請(qǐng)求客戶端的 IP 地址
InetSocketAddress getRemoteAddress?()
// 獲取請(qǐng)求協(xié)議, 例如: HTTP/1.1
String getProtocol?()
// 獲取請(qǐng)求的方法, "GET", "POST" 等
String getRequestMethod?()
// 獲取所有的請(qǐng)求頭
Headers getRequestHeaders?()
// 以輸入流的方式獲取請(qǐng)求內(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)容的長(zhǎng)度
void sendResponseHeaders?(int rCode, long responseLength)
// 獲取響應(yīng)內(nèi)容的輸出流, 響應(yīng)內(nèi)容寫(xiě)到該流
OutputStream getResponseBody?()
// 關(guān)閉處理器, 同時(shí)將關(guān)閉請(qǐng)求和響應(yīng)的輸入輸出流(如果還沒(méi)關(guān)閉)
void close?()
// 獲取此請(qǐng)求對(duì)應(yīng)的上下文對(duì)象
HttpContext getHttpContext?()
// 獲取收到請(qǐng)求的本地地址
InetSocketAddress getLocalAddress?()