如何使用 vibed 检查互联网连接状态?
How to check internet connection status with vibed?
在应用程序启动之前,我需要检查互联网连接是否正常。做这个的最好方式是什么?我正在使用 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.
你需要移动线路
HTTPClientResponse res = requestHTTP("http://www.example.org/");
在 try
块内捕获异常。
如果没有 Internet 连接,则通常没有可用的名称服务。这意味着在没有网络连接的情况下很有可能抛出异常。
在应用程序启动之前,我需要检查互联网连接是否正常。做这个的最好方式是什么?我正在使用 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.
你需要移动线路
HTTPClientResponse res = requestHTTP("http://www.example.org/");
在 try
块内捕获异常。
如果没有 Internet 连接,则通常没有可用的名称服务。这意味着在没有网络连接的情况下很有可能抛出异常。