Solved

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

Posted on 2004-09-25
6
259 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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