當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpServer.create方法代碼示例

本文整理匯總了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();
}
 
開發者ID:saturnism,項目名稱:istio-by-example-java,代碼行數:17,代碼來源:MeetingServer.java

示例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");
    }
}
 
開發者ID:graphflow,項目名稱:graphflow,代碼行數:20,代碼來源:PlanViewerHttpServer.java

示例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.");
    }
}
 
開發者ID:fstab,項目名稱:promagent,代碼行數:18,代碼來源:BuiltInServer.java

示例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();
}
 
開發者ID:osswangxining,項目名稱:iotplatform,代碼行數:17,代碼來源:RestApiCallDemoClient.java

示例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;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:18,代碼來源:HttpNegotiateServer.java

示例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();
}
 
開發者ID:Jugendhackt,項目名稱:OpenVertretung,代碼行數:19,代碼來源:HTTPServer.java

示例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));
}
 
開發者ID:edgexfoundry,項目名稱:export-distro,代碼行數:9,代碼來源:RESTOutboundServiceActivatorTest.java

示例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");
}
 
開發者ID:carlosfaria94,項目名稱:CloudPrime,代碼行數:9,代碼來源:Connection.java

示例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.");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:30,代碼來源:MissingTrailingSpace.java

示例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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:MultiAuthTest.java

示例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();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:bug4714674.java

示例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();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:32,代碼來源:B4769350.java

示例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);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:31,代碼來源:InfiniteLoop.java

示例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);
    }
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:55,代碼來源:SmokeTest.java

示例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);
    }
}
 
開發者ID:wso2-extensions,項目名稱:siddhi-io-http,代碼行數:11,代碼來源:HttpServerListenerHandler.java


注:本文中的com.sun.net.httpserver.HttpServer.create方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。