0

我正在调用这个方法:

ServicePoint sp = ServicePointManager.FindServicePoint(mRequest.RequestUri, this.MapDataWebProxy);

获取服务点,但是当没有可用的互联网连接时,该方法不会返回。

关于如何防止这种情况或设置超时的任何想法?

4

1 回答 1

0

您可以在调用该方法之前尝试验证 Internet 连接。可以这样做:

[DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

    //Creating a function that uses the API function...
    public static bool IsConnectedToInternet()
    {
        int Desc;
        return InternetGetConnectedState(out Desc, 0);
    }

    public ServicePoint GetServicePoint()
    {
        if (!IsConnectedToInternet())
        {
            return null;
        }
        return ServicePointManager.FindServicePoint(mRequest.RequestUri, this.MapDataWebProxy);
    }

也可以通过另一种方式检查 Internet。不使用“wininet.dll”库:使用 .NET 检查 Internet 连接的最佳方法是什么?

于 2019-03-14T14:11:26.617 回答