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


Java HttpServer.stop方法代碼示例

本文整理匯總了Java中com.sun.net.httpserver.HttpServer.stop方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpServer.stop方法的具體用法?Java HttpServer.stop怎麽用?Java HttpServer.stop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.sun.net.httpserver.HttpServer的用法示例。


在下文中一共展示了HttpServer.stop方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: test

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
public static void test() {
    HttpServer server = null;
    try {
        serverDigest = MessageDigest.getInstance("MD5");
        clientDigest = MessageDigest.getInstance("MD5");
        server = startHttpServer();

        int port = server.getAddress().getPort();
        out.println ("Server listening on port: " + port);
        client("http://localhost:" + port + "/chunked/");

        if (!MessageDigest.isEqual(clientMac, serverMac)) {
            throw new RuntimeException(
             "Data received is NOT equal to the data sent");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (server != null)
            server.stop(0);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:ChunkedEncodingTest.java

示例2: main

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
    ResponseCache.setDefault(new ThrowingCache());

    HttpServer server = startHttpServer();
    try {
        URL url = new URL("http://" + InetAddress.getLocalHost().getHostAddress()
                      + ":" + server.getAddress().getPort() + "/NoCache/");
        URLConnection uc = url.openConnection();
        uc.setUseCaches(false);
        uc.getInputStream().close();
    } finally {
        server.stop(0);
        // clear the system-wide cache handler, samevm/agentvm mode
        ResponseCache.setDefault(null);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:17,代碼來源:NoCache.java

示例3: main

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
public static void main(String[] args)
        throws IOException,
        URISyntaxException,
        NoSuchAlgorithmException,
        InterruptedException
{
    HttpServer server = createHttpsServer();
    server.start();
    try {
        test(server, HttpClient.Version.HTTP_1_1);
        test(server, HttpClient.Version.HTTP_2);
    } finally {
        server.stop(0);
        System.out.println("Server stopped");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:ProxyTest.java

示例4: stopHttpServers

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
@AfterClass
public static void stopHttpServers() throws IOException {
    restClient.close();
    restClient = null;
    for (HttpServer httpServer : httpServers) {
        httpServer.stop(0);
    }
    httpServers = null;
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:10,代碼來源:RestClientMultipleHostsIntegTests.java

示例5: create

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
static <T extends HttpServer> T create(HttpProtocolType protocol)
        throws IOException {
    final int max = addresses.size() + MAX;
    final List<HttpServer> toClose = new ArrayList<>();
    try {
        for (int i = 1; i <= max; i++) {
            HttpServer server = newHttpServer(protocol);
            server.bind(new InetSocketAddress("127.0.0.1", 0), 0);
            InetSocketAddress address = server.getAddress();
            String key = address.toString();
            if (addresses.addIfAbsent(key)) {
               System.out.println("Server bound to: " + key
                                  + " after " + i + " attempt(s)");
               return (T) server;
            }
            System.out.println("warning: address " + key
                               + " already used. Retrying bind.");
            // keep the port bound until we get a port that we haven't
            // used already
            toClose.add(server);
        }
    } finally {
        // if we had to retry, then close the servers we're not
        // going to use.
        for (HttpServer s : toClose) {
          try { s.stop(1); } catch (Exception x) { /* ignore */ }
        }
    }
    throw new IOException("Couldn't bind servers after " + max + " attempts: "
                          + "addresses used before: " + addresses);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:32,代碼來源:HTTPTestServer.java

示例6: importFailNot200

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
@Test(expected = PilosaException.class)
public void importFailNot200() throws IOException {
    HttpServer server = runImportFailsHttpServer();
    try (PilosaClient client = PilosaClient.withAddress(":15999")) {
        StaticBitIterator iterator = new StaticBitIterator();
        try {
            client.importFrame(this.index.frame("importframe"), iterator);
        } finally {
            if (server != null) {
                server.stop(0);
            }
        }
    }
}
 
開發者ID:pilosa,項目名稱:java-pilosa,代碼行數:15,代碼來源:PilosaClientIT.java

示例7: test

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
void test(String[] args) throws Exception {
    HttpServer server = startHttpServer();
    CookieHandler previousHandler = CookieHandler.getDefault();
    try {
        InetSocketAddress address = server.getAddress();
        URI uri = new URI("http://" + InetAddress.getLocalHost().getHostAddress()
                          + ":" + address.getPort() + URI_PATH);
        populateCookieStore(uri);
        doClient(uri);
    } finally {
        CookieHandler.setDefault(previousHandler);
        server.stop(0);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:HttpOnly.java

示例8: fail304EmptyResponseTest

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
@Test(expected = PilosaException.class)
public void fail304EmptyResponseTest() throws IOException {
    HttpServer server = runContent0HttpServer("/index/foo", 304);
    try (PilosaClient client = PilosaClient.withAddress(":15999")) {
        try {
            client.createIndex(Index.withName("foo"));
        } finally {
            if (server != null) {
                server.stop(0);
            }
        }
    }
}
 
開發者ID:pilosa,項目名稱:java-pilosa,代碼行數:14,代碼來源:PilosaClientIT.java

示例9: failQueryEmptyResponseTest

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
@Test(expected = PilosaException.class)
public void failQueryEmptyResponseTest() throws IOException {
    String path = String.format("/index/%s/query", this.frame.getIndex().getName());
    HttpServer server = runContent0HttpServer(path, 304);
    try (PilosaClient client = PilosaClient.withAddress(":15999")) {
        try {
            client.query(this.frame.setBit(15, 10));
        } finally {
            if (server != null) {
                server.stop(0);
            }
        }
    }
}
 
開發者ID:pilosa,項目名稱:java-pilosa,代碼行數:15,代碼來源:PilosaClientIT.java

示例10: 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:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:30,代碼來源:MissingTrailingSpace.java

示例11: failStatusEmptyResponseTest

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
@Test(expected = PilosaException.class)
public void failStatusEmptyResponseTest() throws IOException {
    HttpServer server = runContent0HttpServer("/status", 204);
    try (PilosaClient client = PilosaClient.withAddress(":15999")) {
        try {
            client.readStatus();
        } finally {
            if (server != null) {
                server.stop(0);
            }
        }
    }
}
 
開發者ID:pilosa,項目名稱:java-pilosa,代碼行數:14,代碼來源:PilosaClientIT.java

示例12: test

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
void test() throws Exception {
    HttpServer server = null;
    try {
        server = startHttpServer();
        String baseUrl = "http://localhost:" + server.getAddress().getPort() + "/";
        client(baseUrl +  "chunked/");
        client(baseUrl +  "fixed/");
        client(baseUrl +  "error/");
        client(baseUrl +  "chunkedError/");

        // Test with a response cache
        ResponseCache ch = ResponseCache.getDefault();
        ResponseCache.setDefault(new TrivialCacheHandler());
        try {
            client(baseUrl +  "chunked/");
            client(baseUrl +  "fixed/");
            client(baseUrl +  "error/");
            client(baseUrl +  "chunkedError/");
        } finally {
            ResponseCache.setDefault(ch);
        }
    } finally {
        if (server != null)
            server.stop(0);
    }

    System.out.println("passed: " + pass + ", failed: " + fail);
    if (fail > 0)
        throw new RuntimeException("some tests failed check output");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:31,代碼來源:HttpStreams.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), 10);
    ExecutorService e = Executors.newCachedThreadPool();
    Handler h = new Handler();
    HttpContext serverContext = server.createContext("/test", h);
    int port = server.getAddress().getPort();
    System.out.println("Server port = " + port);

    ClientAuth ca = new ClientAuth();
    ServerAuth sa = new ServerAuth("foo realm");
    serverContext.setAuthenticator(sa);
    server.setExecutor(e);
    server.start();
    HttpClient client = HttpClient.newBuilder()
                                  .authenticator(ca)
                                  .build();

    try {
        URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo");
        HttpRequest req = HttpRequest.newBuilder(uri).GET().build();

        HttpResponse resp = client.send(req, asString());
        ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);

        if (!ok || ca.count != 1)
            throw new RuntimeException("Test failed");

        // repeat same request, should succeed but no additional authenticator calls

        resp = client.send(req, asString());
        ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);

        if (!ok || ca.count != 1)
            throw new RuntimeException("Test failed");

        // try a POST

        req = HttpRequest.newBuilder(uri)
                         .POST(fromString(POST_BODY))
                         .build();
        resp = client.send(req, asString());
        ok = resp.statusCode() == 200;

        if (!ok || ca.count != 1)
            throw new RuntimeException("Test failed");
    } finally {
        server.stop(0);
        e.shutdownNow();
    }
    System.out.println("OK");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:52,代碼來源:BasicAuthTest.java

示例14: main

import com.sun.net.httpserver.HttpServer; //導入方法依賴的package包/類
public static void main (String[] args) throws Exception {
    Handler handler = new Handler();
    InetSocketAddress addr = new InetSocketAddress (0);
    HttpServer server = HttpServer.create(addr, 0);
    HttpContext ctx = server.createContext("/test", handler);
    BasicAuthenticator a = new BasicAuthenticator("[email protected]") {
        @Override
        public boolean checkCredentials (String username, String pw) {
            return "fred".equals(username) && pw.charAt(0) == 'x';
        }
    };

    ctx.setAuthenticator(a);
    ExecutorService executor = Executors.newCachedThreadPool();
    server.setExecutor(executor);
    server.start ();
    java.net.Authenticator.setDefault(new MyAuthenticator());

    System.out.print("Deadlock: " );
    for (int i=0; i<2; i++) {
        Runner t = new Runner(server, i);
        t.start();
        t.join();
    }
    server.stop(2);
    executor.shutdown();
    if (error) {
        throw new RuntimeException("test failed error");
    }

    if (count != 2) {
        throw new RuntimeException("test failed count = " + count);
    }
    System.out.println("OK");

}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:37,代碼來源:Deadlock.java

示例15: 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 {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:31,代碼來源:BasicLongCredentials.java


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