1

在应用程序启动之前,我需要检查 Internet 连接是否已启动。做这个的最好方式是什么?我正在使用振动。

我写了下一个代码:

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);
    }
}

但是,如果我在没有 Internet 连接的情况下运行它,我会收到错误:

core.exception.AssertError@core.d(1177): No events registered, exiting event loop.
4

1 回答 1

0

您需要移动线路

HTTPClientResponse res = requestHTTP("http://www.example.org/");

try块内捕获异常。

如果没有互联网连接,则通常没有可用的名称服务。这意味着在没有互联网连接的情况下很有可能抛出异常。

于 2015-07-10T20:27:25.237 回答