Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-09-25
6
258 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
ID: 12162364
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
ID: 12162549
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
ID: 12162674
what do you mean by OR thread?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:lbussy
ID: 12162700
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
ID: 12188570
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
ID: 12189093
Just sorry I couldn't help you more...
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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