如何从互联网获取DateTime?

如何使用C#从Internet或服务器获取当前日期和时间?我正在尝试获取时间,如下所示:


public static DateTime GetNetworkTime (string ntpServer)

{

    IPAddress[] address = Dns.GetHostEntry(ntpServer).AddressList;


    if (address == null || address.Length == 0)

        throw new ArgumentException("Could not resolve ip address from '" + ntpServer + "'.", "ntpServer");


    IPEndPoint ep = new IPEndPoint(address[0], 123);

    return GetNetworkTime(ep);

}

我将服务器IP地址作为传递netServer,但无法正常工作。


慕码人8056858
浏览 261回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP