Solved

WebClient Class and Error Handling (Need assistance making an app more robust

Posted on 2004-09-25
6
251 Views
Last Modified: 2012-05-05
Hello all.  I have beem working sort of half-heartedly on a screen scraper application that downloads a given web page, extracts, formats and presents data.  I have been using the System.Net.WebClient Class to retrieve the web page like so:

Private Function GetPageHTML(ByVal URL As String) As String
    ' Retrieves the HTML from the specified URL
    Dim objWC As New System.Net.WebClient
    Return New System.Text.UTF8Encoding().GetString(objWC.DownloadData(URL))
End Function

It seems to work fairly well except if the page is unavailable or some other network error happens, the application freezes and pretty much never comes back or I get a vague pop-up telling me there was an unhandled exception.  I had done some work in PHP with CURL and it had a timeout value that prevented this but a perusal of the docs does not reveal any such abilities with this class.

So, I'm looking for a way to make a web page retrieval time out and/or return an error if there's an issue.

My forte' is really VB 6.0 so please keep that in mind as you answer.  I'm not very experienced in .NET yet but I'm learning to like it rapidly.

Setting this at 250 because I'm betting that this is an easy answer (if not we can negotiate) but I'm looking for an answer sort of quick before I don;t need it anymore. :-).  Thanks for reading.
0
Comment
Question by:lbussy
  • 3
  • 3
6 Comments
 
LVL 5

Accepted Solution

by:
thenrich earned 250 total points
Comment Utility
Couldn't you do this:

Private Function GetPageHTML(ByVal URL As String) As String
    ' Retrieves the HTML from the specified URL
    Dim objWC As New System.Net.WebClient
try    
Return New System.Text.UTF8Encoding().GetString(objWC.DownloadData(URL))
catch ex as exception
  ex.messge
end try
End Function

Trap the for whatever error you need.
0
 

Author Comment

by:lbussy
Comment Utility
Probably however that only tells the app about an error.  I can't set the timeout value (and prevent the app from hanging) without being able to do that OR thread it somehow.  The timeout havlue as it stands seems like 60 or 120 seconds.
0
 
LVL 5

Expert Comment

by:thenrich
Comment Utility
what do you mean by OR thread?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:lbussy
Comment Utility
Okay, the URL is pulled down triggered by a timer event.  When it tries to pull it down and it fails the timer stops, the application becomes unresponsive, sometimes has to be killed.  Other times it generates a .NET Frameork error.  If it were to pull the data in a different thread somehow or just timeout like I want (say after 10 seconds) then it would be fine.
0
 

Author Comment

by:lbussy
Comment Utility
I'm going to close this and award the points to the only person who took the time to read and try to help.  It's not a solution to my issue but I appreciate the effort anyway.
0
 
LVL 5

Expert Comment

by:thenrich
Comment Utility
Just sorry I couldn't help you more...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now