0

I have a GetLocalTime() method in my Web Service which I want to call in my form.cs and set the time on web server on the device on button click? So what should I write on the button click event to call the datetime function and set the time?

Method in Web Service:

[WebMethod]
        public DateTime GetLocalTime()
        {
            OracleBridge ob = new OracleBridge(_connStr);
            string sqlQuery = "select sysdate from dual";

            DateTime dt = new DateTime();
            try
            {

                dt = Convert.ToDateTime(ob.ExecuteScalar(sqlQuery));

            }
            catch (OracleException ex)
            {
                throw ex;
            }

            return dt;
        }

Button click event in form.cs to get and set the time:

 private void btnSync_Click(object sender, EventArgs e)
        {

                           ????????????               // what should I write here to call the method
                DateTime localDateTime = ?????????;  //what should I write here pass datetime to setsystemdatetime


                SetSystemDateTime(localDateTime);

                System.Threading.Thread.Sleep(500);
                SetSystemDateTime(localDateTime); 
}
Programmermid
  • 588
  • 3
  • 9
  • 30
  • You'd get some code to make an HTTP POST to whatever your webmethod is. I don't know what the file is called where your webmethod is stored, but you'd post to a url such as... `http://server:port/file_with_webmethod.aspx/GetLocalTime` and that should return your DateTime. Maybe look up the `WebRequest` class (https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396). – KSib Jan 27 '17 at 22:08

1 Answers1

0

The .NET CLR does not expose any method for setting the time. If you are hell bent on doing this, you will need to work with the Win32 SetLocalTime API and import it.

I advise against allowing a web server process to set the system clock. When you are able to set the clock, you can cause all sorts of security mechanisms to be bypassed; for example, a malicious user can cause sessions or certificates that are otherwise expired to appear valid.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • I am able to set the time on the device when I hardcode the time and now I just want to pass the value of the datetime function defined in GetLocalTime() to the setlocaltime method. And security is not the issue on the project I am working on. – Programmermid Jan 27 '17 at 20:33