Get date from a website C #

1

I'm developing a C # program that lets you click at a particular time chosen by the user. For now the program clicks by subtracting the time chosen by the user with local time, but I soon discovered that through a Headers website you can tell the exact time of the server, which would facilitate me a lot because the program is being developed to work based on the time of the same.

I use these lines of code to fetch the Headers from the website:

var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.exemplo.com.pt/");
var response = myHttpWebRequest.GetResponse();

To subtract the time entered by the user and the local time I use this line of code:

TimeSpan wait_time = objetivo.Subtract(DateTime.Now); //'objetivo' = hora inserida pelo utilizador

My question is, how can I just fetch the date from the Headers of the website and then subtract it from the time the user chooses?

    
asked by anonymous 19.04.2017 / 14:47

1 answer

0

The response object you use is of type HttpResponse . It has a property called Headers , a collection of values through which it should be possible to read page headers.

Something like:

string dataCabecalho = response.Headers.Get("Date");

Note that the Get method of headers, for the Date header, returns a text. This text should still be turned into date, something like:

DateTime dataRealServidor;
DateTime.TryParse(dataCabecalho, CultureInfo.InvariantCulture.DateTimeFormat, 
DateTimeStyles.None, out dataRealServidor);
    
19.04.2017 / 16:11