当前位置: 首页>>代码示例>>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;未经允许,请勿转载。