本文整理汇总了Java中com.sun.net.httpserver.HttpServer.getAddress方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServer.getAddress方法的具体用法?Java HttpServer.getAddress怎么用?Java HttpServer.getAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.net.httpserver.HttpServer
的用法示例。
在下文中一共展示了HttpServer.getAddress方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
void test(String[] args) throws Exception {
HttpServer server = startHttpServer();
try {
InetSocketAddress address = server.getAddress();
URI uri = new URI("http://" + InetAddress.getLocalHost().getHostAddress()
+ ":" + address.getPort() + "/foo");
doClient(uri);
} finally {
server.stop(0);
}
}
示例2: 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);
}
}
示例3: 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);
}
}
示例4: 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);
}
示例5: test
import com.sun.net.httpserver.HttpServer; //导入方法依赖的package包/类
void test(String[] args) throws Exception {
HttpServer server = startHttpServer();
try {
InetSocketAddress address = server.getAddress();
URI uri = new URI("http://localhost:" + address.getPort() + "/foo");
doClient(uri);
} finally {
server.stop(0);
}
}