在应用程序启动之前,我需要检查互联网连接是否已经启动。做这件事最好的方法是什么?我用的是vibed。
我写了下一段代码:
import std.stdio;
import std.string;
import std.conv;
import vibe.d;
import vibe.core.log;
import vibe.http.client;
import vibe.stream.operations;
import vibe.core.driver;
void main()
{
HTTPClientResponse res = requestHTTP("http://www.example.org/");
try
{
if (res.statusCode != 200)
{
logInfo("Check Internet connection, and try again!");
logInfo("Server response is: " ~ to!string(res.statusCode));
}
else
{
logInfo("All ok");
}
}
catch (Exception e)
{
writeln(e.msg);
}
}但是如果我运行它没有互联网连接,我就会出错:
core.exception.AssertError@core.d(1177): No events registered, exiting event loop.发布于 2015-07-10 20:27:25
你得把线移开
HTTPClientResponse res = requestHTTP("http://www.example.org/");在try块内捕获异常。
如果没有互联网连接,那么通常就没有名称服务可用。这意味着在没有互联网连接的情况下抛出异常的可能性很高。
https://stackoverflow.com/questions/31097992
复制相似问题