Can't seem to make URL request from ElapsedEventHandler

AI Thread Summary
The discussion revolves around a Windows Service method, LoadAndSavePage, that is intended to execute every second but fails to log output when attempting to download a web page. Although the method is confirmed to be triggered, it does not reach the point of logging the downloaded HTML content, suggesting a potential issue related to web requests. The user has experimented with various libraries, including .NET's WebClient and HtmlAgilityPack, and has ruled out event resolution time as a factor. The conversation hints at a possible permissions issue, with a suggestion to run the service under the NetworkService account to resolve the problem.
SlurrerOfSpeech
Messages
141
Reaction score
11
I've spent 10+ hours on trying to solve this and can't get it to work!

In short, I have a method

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
             // ... 
        }

which is being called every 1 second when the Windows Service that contains it is running. I've confirmed that it is indeed being called; I can verify with

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            EventLog.WriteEntry("I got called!");
        }

However, as soon as I try to get a web page, for example

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            string html = new WebClient().DownloadString("[PLAIN]http://physicsforums.com");[/PLAIN] 
            EventLog.WriteEntry(html);
        }

it doesn't work. The function gets called as scheduled but it never gets to the point where it writes to the event log. Something about introducing a web request messes it up. I'm guessing maybe some type of permissions issue?

I've tried with several libraries including .NET's WebClient and HtmlAgilityPack's HtmlWeb, I've tried with different URLs, etc.

I know it doesn't have to do with the event having a maximum resolution time, for I've also tried

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            EventLog.WriteEntry("before sleep");
            Thread.Sleep(1500);
            EventLog.WriteEntry("after sleep");
        }

and confirmed that the whole thing runs.
 
Last edited by a moderator:
Technology news on Phys.org
SlurrerOfSpeech said:
However, as soon as I try to get a web page, for example

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            string html = new WebClient().DownloadString("[PLAIN]http://physicsforums.com");[/PLAIN] 
            EventLog.WriteEntry(html);
        }

it doesn't work.

The function gets called as scheduled but it never gets to the point where it writes to the event log. Something about introducing a web request messes it up. I'm guessing maybe some type of permissions issue?
That would be my guess, but that's just a guess. In your following post, you suggest running the service as a NetworkService. I would give that a shot.
 
Last edited by a moderator:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top