Net.WebClient()

Robby

Ultimate Contributor
Joined
Nov 17, 2002
Messages
3,460
Location
Montreal, Ca.
The following code goes to a url, reads the source and if all is
good, it then downloads an image from that server(site).
It works well, but only 80% of the time.

I am always on-line, but once in a while it hangs for 30 seconds
trying to establish an internet connection.

Either there is something wrong in my method, or maybe I should
poll my connection prior to running the code. Any thoughts.

btw, I have the same logic working perfectly in VB6, using an Inet
control.
Visual Basic:
Dim sHtml As String
Dim sr As IO.StreamReader
Dim wc As New Net.WebClient()

sr = New IO.StreamReader(wc.OpenRead(m_URL))
sHtml = sr.ReadToEnd
sr.DiscardBufferedData()
sr.Close()
wc.Dispose()

'if the above succeeds then ....
Dim wc As New Net.WebClient()
wc.DownloadFile(URL_IMAGE & sDir & "/" & sName, sName)
wc.Dispose()
SetImageBox(sName)
 
Last edited:
The WebClient class is just a basic reimplementation of the WebRequest and WebResponse classes, which provide asynchronous methods that can be used to prevent application hangs.

I'd test your code out with other servers to see if this is an isolated issue, otherwise I'd go ahead and implement the async methods, above.
 
It's probably not establishing a connection, but it's waiting for one.
The same as when you open IE but you're not on-line, it may wait
for a while (not always 30 seconds though).

I have a VB6 version which works 100% of the time, so I'm doubting it's the server.

The asynch is an idea, I'll give it a go.

Thanks.
 
Hmm, When I disable my fire-wall it works great, so I changed the version number in the AssemblyInfo to a fixed value and all is well.

Thanks.
 
I'm an idiot for not figuring it out sooner (even though I started
this project only yesterday), because in the past, any project that
used SqlClient would cause the fire-wall to prompt (allow /
disallow) after each compile, to get around this I would change
the incrementing version number to a fixed number.

I had assumed that once WebClient was passed the fire-wall any
subsequent request would be OK'ed as well. Live and learn :)

Thanks for the idea Derek, I may still use asynch approach.
 
Back
Top