4

サーバーの時間外に出ようとしています。

まず、私はそれを試しました:

string URL = "http://google.com"; 
System.Net.HttpWebRequest rq2 = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);
System.Net.HttpWebResponse res2 = (System.Net.HttpWebResponse)rq2.GetResponse();
DateTime xd = rq2.Date;

しかし、これは遅く、ミリ秒が含まれていません。

次に、ターゲットサーバーで使用されているntpサーバーから実際にサーバーの時間を取得できることがわかりました。

 InternetTime.SNTPClient sntp = new InternetTime.SNTPClient("time-b.timefreq.bldrdoc.gov");
 sntp.Connect(false); // true to update local client clock
 DateTime dt = sntp.DestinationTimestamp.AddMilliseconds(sntp.LocalClockOffset);
 string timeStampNow = dt.ToString("dd/MM/yyyy HH:mm:ss.fff");

それはかなりうまく機能しますが、私のターゲット サーバーが ntp サーバーを使用しているかどうかはわかりません。かなり時間をかけて調べたのでこちらで質問させていただきます。

edit2:私はそのサーバーにアクセスできません(wwwページにアクセスする以外に)、サーバーがMicrosoft-IIS/7.5 web serverを使用していることは知っています。

4

1 に答える 1

3

There is no way to reliably get a random HTTP server's time with more resolution than what it returns in the response header. That, as you've found, is accurate only to seconds. And it's not the current time, but rather the time that the server populated the header. You don't know if it's the time that the request came in, the time that the response was sent, or possibly some random time that some goofy programmer stuffed into the header just to make your life difficult. All you can say is, "this is the time the server reported."

Even if you knew what NTP server the server in question gets its time from, and assuming that you could access that server, getting time from it won't give you an accurate value for the server's time. Servers update their clocks via NTP periodically, but that might be daily, weekly, monthly, or "whenever the admin decides to." The NTP server's time will not accurately reflect the particular server's time. Certainly not in the milliseconds range, in the general case. And possibly not even within minutes if the server hasn't synchronized its clock with the NTP server in some time.

By the way, if all you're looking for is the time that the server returns, you probably should do a HEAD request if possible. But you have to support doing a GET request as well, because many servers will return a 405 error in response to a HEAD request.

于 2013-03-01T19:32:02.757 に答える