0

I want to try get current DateTime like this :

try {
    HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.timestampconvert.com/");
    req.Method = "GET";
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36";
    req.ContentType = "text/html; charset=utf-8";
    req.Referer = string.Empty;
    req.KeepAlive = true;
    req.Timeout = 25000;

    //req.Proxy = proxy;
    HttpWebResponse res = (HttpWebResponse) req.GetResponse();

    Stream Stream = res.GetResponseStream();
    StreamReader reader = new StreamReader(Stream);
    string reader_str = reader.ReadToEnd();

    HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
    htmlDoc.LoadHtml(reader_str);

    var divs = from div in htmlDoc.DocumentNode.Descendants("div")
    select div;

    DateTime dt = DateTime.Now;

    foreach(var div in divs) {
        if (div.Attributes["class"] != null) {
            if (div.Attributes["class"].Value == "remarkrow") {
                if (div.InnerText.Contains("Computation based on input date")) {
                    CultureInfo cultureinfo = new CultureInfo("en-US");
                    dt = Convert.ToDateTime(div.InnerText.Replace("\n\t*)Computation based on input date ", string.Empty), cultureinfo);
                    dt = dt.AddHours(2).AddMinutes(30);
                    break;
                }
            }
        }
    }

    DateTime dt_ = dt;
}
catch(Exception ex) {
    MessageBox.Show(ex.ToString());
}

But it has an exception like below :

The request was aborted: Could not create SSL/TLS secure channel

How can i fix this error?

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • That page doesn't even provide the current date. It's a time converter. I'm sure there must be other pages / APIs which would give you the time directly, maybe in a JSON object. – ADyson Dec 22 '20 at 14:19
  • Does this answer your question? [The request was aborted: Could not create SSL/TLS secure channel](https://stackoverflow.com/questions/2859790/the-request-was-aborted-could-not-create-ssl-tls-secure-channel) – canton7 Dec 22 '20 at 14:24
  • How can i disable SSL/TLS certificate check in codes? Would you please introduce me a free api to get current datetime! I was using that site for several years - because it was so light & fast. – SilverLight Dec 22 '20 at 14:36
  • Add following to beginning of your code (after the request) :ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; – jdweng Dec 22 '20 at 14:37
  • 2
    Surely they'd want that *before* the request...? – canton7 Dec 22 '20 at 14:42
  • 1
    The server setup is broken. It is not providing the full certificate chain but only the leaf certificate. See [SSLLabs](https://www.ssllabs.com/ssltest/analyze.html?d=www.timestampconvert.com) and look for *"Chain issues - incomplete"*. And no, this can not be fixed by enforcing TLS 1.2 as suggested. – Steffen Ullrich Dec 22 '20 at 14:43
  • By adding this code : `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;` at the beginning exception vanished. But what is this code exactly & what do it do? – SilverLight Dec 22 '20 at 14:48
  • It sets the TLS version – ADyson Dec 22 '20 at 14:50
  • I do n't want that code affect on all of my codes! i want to change SecurityProtocol of that request only, not other requests. – SilverLight Dec 22 '20 at 14:53
  • Why do you think it would affect anything other than this request? – Caius Jard Dec 22 '20 at 15:09
  • 1
    free api to get current datetime - how about from an NTP server instead of this awful "scrape some html off a server that puts the time in a webpage" way? https://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c – Caius Jard Dec 22 '20 at 15:11
  • NTP server > All those codes have deprecated & do n't work any more.http://time.windows.com = Not Found | pool.ntp.org = needs user name & password – SilverLight Dec 22 '20 at 15:37
  • That's because you're trying to use them via HTTP instead of NTP. Read the link that Caius Jard gave you, it shows how to do it properly – ADyson Dec 22 '20 at 17:34

0 Answers0