Net.WebClient()

Robby

Moderator
Joined
Nov 17, 2002
Messages
3,461
Location
Montreal, Ca.
User Rank
*Expert*
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.
Code:
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 by a moderator:
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.

Id test your code out with other servers to see if this is an isolated issue, otherwise Id go ahead and implement the async methods, above.
 
I am always on-line, but once in a while it hangs for 30 seconds
trying to establish an internet connection.
Why would it be trying to establish a connection if youre already online? Im guessing you are as clueless as I am.
 
Its probably not establishing a connection, but its waiting for one.
The same as when you open IE but youre 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 Im doubting its the server.

The asynch is an idea, Ill 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.
 
Im 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 OKed as well. Live and learn :)

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