Can't seem to make URL request from ElapsedEventHandler

In summary, the conversation is discussing a problem with a method being called every 1 second in a Windows Service. The method is not functioning properly when trying to make a web request, possibly due to permissions issues. The suggestion is to try running the service as a NetworkService.
  • #1
SlurrerOfSpeech
141
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
  • #3
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:

FAQ: Can't seem to make URL request from ElapsedEventHandler

1. Why am I unable to make a URL request from ElapsedEventHandler?

There could be several reasons for this issue. It could be due to incorrect syntax in your code, network connectivity issues, or server-side errors. It is important to carefully check your code and make sure all necessary parameters are included in the request.

2. How can I troubleshoot the issue of not being able to make a URL request from ElapsedEventHandler?

To troubleshoot this issue, you can start by checking your code for any syntax errors or missing parameters. You can also try testing your request using a different network or device to see if the issue persists. Additionally, you can check the server logs for any errors that may have occurred during the request.

3. Is there a specific format or protocol that needs to be followed when making a URL request from ElapsedEventHandler?

Yes, there are certain protocols and formats that need to be followed when making a URL request. For example, you should specify the correct HTTP method (GET, POST, etc.), include the necessary headers and parameters, and ensure that the URL is properly encoded.

4. Can the problem of not being able to make a URL request from ElapsedEventHandler be caused by server-side issues?

Yes, server-side issues can also be a cause for this problem. If the server is down or experiencing high traffic, it may not be able to process your request. It is recommended to check the server status and logs to see if there are any errors that could be causing the issue.

5. Are there any alternative methods to make a URL request instead of using ElapsedEventHandler?

Yes, there are alternative methods to make a URL request, such as using the WebClient class or the HttpWebRequest class. You can also try using a different programming language or framework that may have better support for making HTTP requests.

Similar threads

Back
Top