0

I made a simple html testpage. If the page is opened (http://localhost/cut/public/updateFile) then I send a timestamp to my webserver, where the value is written into a file date.txt e.g.:

4/8/2017 @ 14:50:19

I also wrote a C# Windows Forms App which makes a request to the webserver to get the value of this file (date.txt), every X seconds.

My goal is to show the current time in a notification box every X seconds by reading it from the file on the server (for practice only)

However, before I get the file content, I need to update it first of course to get the current date and time.

Is it possible to solve this with the rules defined above? This is my attempt:

    private void timerA_Tick(object sender, EventArgs e)
    {
        sendWebRequest("http://localhost/cut/public/updateFile");
        timerA.Stop();
        timerA2.Interval = 5000;
        timerA2.Start();
    }


    private void timerA2_tick(object sender, EventArgs e)
    {

        //Returns the content of date.txt
        string response = sendWebRequest("http://localhost/cut/public/fileApi?action=read&targetFile=date");  

        //Show Notification
        notifyIcon1.Visible = true;
        notifyIcon1.Icon = SystemIcons.Exclamation;
        notifyIcon1.BalloonTipTitle = "File content";
        notifyIcon1.BalloonTipText = response;
        notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
        notifyIcon1.ShowBalloonTip(10000);

        timerA2.Stop();
        timerA.Start();
    }

    /**
     * Send request and get response as string
     */
    public static string sendWebRequest(string URL)
    {
        WebRequest request = WebRequest.Create(URL);
        WebResponse response = request.GetResponse();

        StreamReader streamReader = new StreamReader(response.GetResponseStream());

        return (string)streamReader.ReadToEnd();

    }

However I always get 4/8/2017 @ 14:50:19 it does not update as It should.

It obviously does not work this way, since WebRequest.Create does only gets the html file as it is and delivers it back to my C# Application, but it does not execute the javascript where I make the request to the server.

I only created this example above to ask if it is somehow possible to achive this at this way or if C# is not designed to solve problems like this?

My only idea is to create a hidden webbrowser in my Form1 and open http://localhost/cut/public/updateFile to start the update but I am not sure if this even works.


enter image description here

I created a webbrowser element and call the update URL like this:

webBrowser1.Navigate("http://localhost/cut/public/updateFile");

However, there are plenty of script error messages, which are found in the jquery file.

enter image description here

And my script won't work either because of the not working jquery. So I guess it would work, but not with jquery, or I have to fix all errors in the jQuery file.

How to solve this problem?

Black
  • 18,150
  • 39
  • 158
  • 271
  • `webBrowser1.ScriptErrorsSuppressed = true;` - set this and try again - https://stackoverflow.com/questions/2476360/disable-javascript-error-in-webbrowser-control – Subbu Aug 04 '17 at 14:16
  • @Subbu, already tried, but it does not help since the scripts are still not working then. – Black Aug 04 '17 at 14:19

2 Answers2

0

You should open http://localhost/cut/public/updateFile from a real browser, which will execute the javascript on the page. Requesting this page from a windows form application with a WebRequest will just return the contents of the page, but will not process or execute the javascript on the page because it is not rendered or processed.

Frits
  • 314
  • 1
  • 12
  • But I try to automate the whole process without any user interaction, I don't want the user to refresh the page every 5 seconds... there must be a way. I just tried it with the C# webbrowser, but I only get it to work If i open the browser externaly with `webBrowser1.Navigate("http://localhost/cut/public/updateFile", true);` but this is not how it should be, no one wants windows popping up all the time. – Black Aug 04 '17 at 13:59
  • 1
    Another approach could be to write server-side code with C#, which will be executed upon the request of a prepared page, or writing an http-module. – Frits Aug 05 '17 at 15:20
0

I solved it by adding a webbrowser element to my Form, and calling the URL inside of it like this:

    webBrowser1.Navigate("http://localhost/cut/public/updateFile");

However, I had to rewrote my javascript to make it work without jQuery, since the C# Webbrowser appears to be an extremly old Internet Explorer with no support for nothing. There were plenty alerts pointing to script errors like the one below:

enter image description here

Now it works as expected. I hope someday somebody will come across with a much better solution than this though.

UPDATE

I was able to change the browser to the latest available by adding this line to my html head section:

<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<!-- IE=edge instructs the WebBrowser control to use the latest version of IE it supports -->
Black
  • 18,150
  • 39
  • 158
  • 271