本文整理汇总了Java中com.sun.net.httpserver.HttpServer.create方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServer.create方法的具体用法?Java HttpServer.create怎么用?Java HttpServer.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.net.httpserver.HttpServer
的用法示例。
在下文中一共展示了HttpServer.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8081), 0);
server.setExecutor(Executors.newCachedThreadPool());
server.createContext("/meet", exchange -> {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
}
exchange.sendResponseHeaders(200, 0);
exchange.getResponseBody().close();
});
server.start();
}
示例2: run
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
/**
* Starts the http server for Graphflow web UI.
*/
@Override
public void run() {
try {
HttpServer server = HttpServer.create(new InetSocketAddress(HTTP_HOST, HTTP_PORT), 0);
// Create a route for input query
server.createContext("/query", new PlanViewerHttpHandler());
// Create a default executor
server.setExecutor(null);
server.start();
File webViewer = new File(PLAN_VIEWER_HTML_PATH);
logger.info("Please open the Graphflow UI (link below) in a browser:");
logger.info("file://" + webViewer.getAbsolutePath());
} catch (IOException exception) {
logger.error("GraphflowUIHttpServer: failed to start");
}
}
示例3: run
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
static void run(String host, String portString, CollectorRegistry registry) throws Exception {
try {
int port = Integer.parseInt(portString);
InetSocketAddress address = host == null ? new InetSocketAddress(port) : new InetSocketAddress(host, port);
HttpServer httpServer = HttpServer.create(address, 10);
httpServer.createContext("/", httpExchange -> {
if ("/metrics".equals(httpExchange.getRequestURI().getPath())) {
respondMetrics(registry, httpExchange);
} else {
respondRedirect(httpExchange);
}
});
httpServer.start();
} catch (NumberFormatException e) {
throw new RuntimeException("Failed to parse command line arguments: '" + portString + "' is not a valid port number.");
}
}
示例4: main
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(HTTP_SERVER_PORT), 0);
HttpContext secureContext = server.createContext(DEMO_REST_BASIC_AUTH, new RestDemoHandler());
secureContext.setAuthenticator(new BasicAuthenticator("demo-auth") {
@Override
public boolean checkCredentials(String user, String pwd) {
return user.equals(USERNAME) && pwd.equals(PASSWORD);
}
});
server.createContext(DEMO_REST_NO_AUTH, new RestDemoHandler());
server.setExecutor(null);
System.out.println("[*] Waiting for messages.");
server.start();
}
示例5: httpd
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
/**
* Creates and starts an HTTP or proxy server that requires
* Negotiate authentication.
* @param scheme "Negotiate" or "Kerberos"
* @param principal the krb5 service principal the server runs with
* @return the server
*/
public static HttpServer httpd(String scheme, boolean proxy,
String principal, String ktab) throws Exception {
MyHttpHandler h = new MyHttpHandler();
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
HttpContext hc = server.createContext("/", h);
hc.setAuthenticator(new MyServerAuthenticator(
proxy, scheme, principal, ktab));
server.start();
return server;
}
示例6: start
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
public static void start() throws Exception
{
database = new Db();
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
NetworkInterface nInterface = interfaces.nextElement();
Enumeration<InetAddress> adresses = nInterface != null ? nInterface.getInetAddresses() : null;
InetAddress iAdress = null;
while (adresses != null && adresses.hasMoreElements())
iAdress = adresses.nextElement();
String adress = iAdress != null ? iAdress.getHostAddress() : "";
server = HttpServer.create(new InetSocketAddress(adress,
8001), 0);
server.createContext("/test", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
示例7: testRESTOutbound
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
@Test
public void testRESTOutbound() throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(RegistrationData.TEST_PORT), 0);
server.createContext("/", new TestHandler());
server.setExecutor(null);
server.start();
assertEquals("Returned event id does not match expected", TEST_ID, activator.restOutbound(message));
}
示例8: setupServer
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
public static void setupServer() throws IOException
{
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/f.html", new LoadBalancer.MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
System.out.println("Load Balancer ready");
}
示例9: main
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
try {
server.setExecutor(Executors.newFixedThreadPool(1));
server.createContext(someContext, new HttpHandler() {
@Override
public void handle(HttpExchange msg) {
try {
try {
msg.sendResponseHeaders(noMsgCode, -1);
} catch(IOException ioe) {
ioe.printStackTrace();
}
} finally {
msg.close();
}
}
});
server.start();
System.out.println("Server started at port "
+ server.getAddress().getPort());
runRawSocketHttpClient("localhost", server.getAddress().getPort());
} finally {
((ExecutorService)server.getExecutor()).shutdown();
server.stop(0);
}
System.out.println("Server finished.");
}
示例10: createServer
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
static HttpServer createServer(ExecutorService e, BasicAuthenticator sa) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 10);
Handler h = new Handler();
HttpContext serverContext = server.createContext("/test", h);
serverContext.setAuthenticator(sa);
server.setExecutor(e);
server.start();
return server;
}
示例11: DeafServer
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
/**
* Create and start the HTTP server.
*/
public DeafServer() throws IOException {
InetSocketAddress addr = new InetSocketAddress(0);
server = HttpServer.create(addr, 0);
HttpHandler handler = new DeafHandler();
server.createContext("/", handler);
server.setExecutor(Executors.newCachedThreadPool());
server.start();
}
示例12: startServer
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
public void startServer() {
InetSocketAddress addr = new InetSocketAddress(0);
try {
server = HttpServer.create(addr, 0);
} catch (IOException ioe) {
throw new RuntimeException("Server could not be created");
}
executor = Executors.newFixedThreadPool(10);
server.setExecutor(executor);
server.createContext("/test/realm1/t1a",
new AuthenticationHandlerT1a() );
server.createContext("/test/realm2/t1b",
new AuthenticationHandlerT1b());
server.createContext("/test/realm1/t1c",
new AuthenticationHandlerT1c());
server.createContext("/test/realm2/t1d",
new AuthenticationHandlerT1d());
server.createContext("/test/realm3/t2a",
new AuthenticationHandlerT2a());
server.createContext("/test/realm3/t2b",
new AuthenticationHandlerT2b());
server.createContext("/test/realm4/t3a",
new AuthenticationHandlerT3a());
server.createContext("/test/realm4/t3b",
new AuthenticationHandlerT3bc());
server.createContext("/test/realm4/t3c",
new AuthenticationHandlerT3bc());
t1Cond1 = new CyclicBarrier(3);
server.start();
}
示例13: main
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/test/InfiniteLoop", new RespHandler());
server.start();
try {
InetSocketAddress address = server.getAddress();
URL url = new URL("http://localhost:" + address.getPort()
+ "/test/InfiniteLoop");
final Phaser phaser = new Phaser(2);
for (int i=0; i<10; i++) {
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
final InputStream is = uc.getInputStream();
final Thread thread = new Thread() {
public void run() {
try {
phaser.arriveAndAwaitAdvance();
while (is.read() != -1)
Thread.sleep(50);
} catch (Exception x) { x.printStackTrace(); }
}};
thread.start();
phaser.arriveAndAwaitAdvance();
is.close();
System.out.println("returned from close");
thread.join();
}
} finally {
server.stop(0);
}
}
示例14: initServer
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
static void initServer() throws Exception {
Logger logger = Logger.getLogger("com.sun.net.httpserver");
ConsoleHandler ch = new ConsoleHandler();
logger.setLevel(Level.SEVERE);
ch.setLevel(Level.SEVERE);
logger.addHandler(ch);
String root = System.getProperty ("test.src")+ "/docs";
InetSocketAddress addr = new InetSocketAddress (0);
s1 = HttpServer.create (addr, 0);
if (s1 instanceof HttpsServer) {
throw new RuntimeException ("should not be httpsserver");
}
s2 = HttpsServer.create (addr, 0);
HttpHandler h = new FileServerHandler(root);
HttpContext c1 = s1.createContext("/files", h);
HttpContext c2 = s2.createContext("/files", h);
HttpContext c3 = s1.createContext("/echo", new EchoHandler());
redirectHandler = new RedirectHandler("/redirect");
redirectHandlerSecure = new RedirectHandler("/redirect");
HttpContext c4 = s1.createContext("/redirect", redirectHandler);
HttpContext c41 = s2.createContext("/redirect", redirectHandlerSecure);
HttpContext c5 = s2.createContext("/echo", new EchoHandler());
HttpContext c6 = s1.createContext("/keepalive", new KeepAliveHandler());
redirectErrorHandler = new RedirectErrorHandler("/redirecterror");
redirectErrorHandlerSecure = new RedirectErrorHandler("/redirecterror");
HttpContext c7 = s1.createContext("/redirecterror", redirectErrorHandler);
HttpContext c71 = s2.createContext("/redirecterror", redirectErrorHandlerSecure);
delayHandler = new DelayHandler();
HttpContext c8 = s1.createContext("/delay", delayHandler);
HttpContext c81 = s2.createContext("/delay", delayHandler);
executor = Executors.newCachedThreadPool();
s1.setExecutor(executor);
s2.setExecutor(executor);
ctx = new SimpleSSLContext().get();
sslparams = ctx.getSupportedSSLParameters();
s2.setHttpsConfigurator(new Configurator(ctx));
s1.start();
s2.start();
port = s1.getAddress().getPort();
System.out.println("HTTP server port = " + port);
httpsport = s2.getAddress().getPort();
System.out.println("HTTPS server port = " + httpsport);
httproot = "http://127.0.0.1:" + port + "/";
httpsroot = "https://127.0.0.1:" + httpsport + "/";
proxy = new ProxyServer(0, false);
proxyPort = proxy.getPort();
System.out.println("Proxy port = " + proxyPort);
}
示例15: run
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
@Override
public void run() {
try {
server = HttpServer.create(new InetSocketAddress(port), 5);
server.createContext("/abc", serverListener);
server.start();
} catch (IOException e) {
logger.error("Error in creating test server.", e);
}
}