我需要开发一个从NTP服务器获取当前时间的应用程序,但在Windows 8 Store App中找不到任何示例。如果我尝试使用正常的C#类,它不起作用。有谁知道如何解决这个问题?在Windows 8 App中使用C#从NTP服务器获取时间应用
2
A
回答
1
我认为这是你想要的。
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
private async Task<DateTime?> GetNistTime()
{
DateTime? dateTime = null;
HttpClient httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://nist.time.gov/timezone.cgi?UTC/s/0"));
HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
string text = await httpResponseMessage.Content.ReadAsStringAsync();
if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
{
string html = await httpResponseMessage.Content.ReadAsStringAsync();
string time = Regex.Match(html, @">\d+:\d+:\d+<").Value; //HH:mm:ss format
string date = Regex.Match(html, @">\w+,\s\w+\s\d+,\s\d+<").Value; //dddd, MMMM dd, yyyy
dateTime = DateTime.Parse((date + " " + time).Replace(">", "").Replace("<", ""));
}
return dateTime;
}
+0
'HttpClient'有一个'GetStringAsync' - 不需要'HttpRequestMessage' /'HttpResponseMessage'开销。除此之外 - 当页面结构发生变化时,直接字符串解析在长期运行中势必会引发问题。 – 2013-05-05 20:17:18
0
您需要一个StreamSocket,然后自己实施NTP网络协议。如果您有经典Windows的现有NTP C#类,则可以改为使用代码StreamSocket。
2
我强烈建议避免字符串解析出HTML页面 - 轻微的视图格式更改会破坏您的应用程序。
基于在this answer提供的示例中,这里是DatagramSocket适应得到适当DateTime
对象:
DatagramSocket socket = new DatagramSocket();
socket.MessageReceived += socket_MessageReceived;
await socket.ConnectAsync(new HostName("time.windows.com"), "123");
using (DataWriter writer = new DataWriter(socket.OutputStream))
{
byte[] container = new byte[48];
container[0] = 0x1B;
writer.WriteBytes(container);
await writer.StoreAsync();
}
当接收到消息时,可以通过一个内置处理传入的字节数组在阅读器:
void socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
using (DataReader reader = args.GetDataReader())
{
byte[] b = new byte[48];
reader.ReadBytes(b);
DateTime time = GetNetworkTime(b);
}
}
GetNetworkTime
是几乎相同的,如我所提到的例子中,用作为arg的一个传递的缓冲区请注意:
public static DateTime GetNetworkTime(byte[] rawData)
{
//Offset to get to the "Transmit Timestamp" field (time at which the reply
//departed the server for the client, in 64-bit timestamp format."
const byte serverReplyTime = 40;
//Get the seconds part
ulong intPart = BitConverter.ToUInt32(rawData, serverReplyTime);
//Get the seconds fraction
ulong fractPart = BitConverter.ToUInt32(rawData, serverReplyTime + 4);
//Convert From big-endian to little-endian
intPart = SwapEndianness(intPart);
fractPart = SwapEndianness(fractPart);
var milliseconds = (intPart * 1000) + ((fractPart * 1000)/0x100000000L);
//**UTC** time
var networkDateTime = (new DateTime(1900, 1, 1)).AddMilliseconds((long)milliseconds);
return networkDateTime;
}
// stackoverflow.com/a/3294698/162671
static uint SwapEndianness(ulong x)
{
return (uint)(((x & 0x000000ff) << 24) +
((x & 0x0000ff00) << 8) +
((x & 0x00ff0000) >> 8) +
((x & 0xff000000) >> 24));
}
相关问题
- 1. 获取NTP - 服务器的时间从Android应用程序
- 2. 从time.nist.gov获取时间使用JavaScript的NTP服务器?
- 3. Android从ntp服务器获取UTC时间
- 4. 在Java应用程序中调度任务时如何使用NTP服务器?
- 5. 在Windows 7上设置NTP服务器
- 6. 在C#中使用WMI获取Windows服务器状态
- 7. ntp服务的使用
- 8. 使用VB.NET查询NTP服务器
- 9. 在Windows Phone 8中使用Wcf服务
- 10. 如何从使用AJAX的其他服务器获取时间?
- 11. 如何使用App Tethering从服务器获取图像
- 12. 如何使用Internet时间服务器获取时间?
- 13. 在Windows服务中使用计时器
- 14. 当使用亚马逊的EC2服务来对抗时钟漂移时,我应该使用NTP服务器吗?
- 15. 从NTP服务器向strftime函数传递时间信息
- 16. 如何使用Firestore获取Firebase云服务器中的服务器时间戳?
- 17. 从Windows服务获取%APPDATA%[C++]
- 18. 在Windows 8应用程序中使用C#读取Base64图像
- 19. 在Android应用中禁用注销按钮一段时间,并从服务器获取时间
- 20. 从服务器获取的时间戳本地时间戳
- 21. 使用Windows服务器2008读取Windows服务中的设置属性(C#)
- 22. 从C#驱动程序获取MongoDB服务器时间
- 23. 如何从服务器获取用户的午夜时间?
- 24. 在Javascript请求后使用Javascript从服务器获取响应
- 25. 在Windows 8应用程序中从Goodreads获取访问令牌
- 26. 从服务器获取Windows登录名
- 27. php验证NTP服务器
- 28. 启动/停止服务用C#Windows 8应用
- 29. 为什么我在使用C#的Windows Phone 8中获取FileNotFoundException?
- 30. 使用XMLHttpRequest从服务器获取数据(适用于Phonegapp BlackBerry App)
“它不起作用”对问题的描述过于模糊。请编辑您的问题以更具体。 – 2013-04-09 20:17:23
可能重复的[如何使用C#查询NTP服务器?](http://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c) – Nasreddine 2015-06-30 15:05:26