Test if image at a uri location is available

  • Thread starter Thread starter B.J. Raiford
  • Start date Start date
B

B.J. Raiford

I have a web site that shows the weather for a location and I have a list of
weather sites that I regularly used ordered by preference. I had to do this
because the location that the client likes most goes down all of the time
and I get crap for it not working correctly. So basically I want to try a
list of images and display the first one that appears, the code I have below
works be it is way to slow because it waits for a network timeout to occur
and a WebException to be thrown before moving to the next item. Does anyone
have another idea of how this might be accomplished?

Private Sub LoadWeatherData()
Dim WeatherLinkPre(2) As String
Dim zipCode As String
Dim i As Integer

WeatherLinkPre(0) =
"http://www.weatherroom.com/weather?forecast=hourly&hwvtc=FFFF40&hwvbg=3E6DF
2&config=png&alt=hwicc&pands="
WeatherLinkPre(1) =
"http://www.weatherusa.net/forecasts/?forecast=hourly&alt=hwicc&config=png&p
ands="
WeatherLinkPre(2) =
"http://wxport.accuweather.com/wxpost/graphic.aspx?type=31&partner=wwwlink&z
ipcode="

'zip code is gathered from the database in my app
zipCode = DbConn.GetZip()



Dim testWB As New System.Net.WebClient()

For i = 0 To WeatherLinkPre.Length - 1 Step 1
Try
testWB.DownloadData(WeatherLinkPre(i) & zipCode)
_weatherLink = WeatherLinkPre(i) & zipCode
Exit For
Catch e As System.Net.WebException
'ignore exception and move on to next option
End Try
Next
End Sub
 
BJ,

I copied this from a program, so it is not a complete sample however I made
some changes trying to make it more clear. There is too much in it, you
don't probably not need the values, only if it exist.

\\\
Dim wbRq As HttpWebRequest = DirectCast(WebRequest.Create("UrlString"),
HttpWebRequest)
wbRq.Timeout = 2000
Try
Dim wbRs As HttpWebResponse = DirectCast(wbRq.GetResponse(),
HttpWebResponse)
Dim wbHCol As WebHeaderCollection = wbRs.Headers
For i As Integer = 0 To wbHCol.Count - 1
Dim header As String = wbHCol.GetKey(i)
Dim values As String() = wbHCol.GetValues(header)
If values.Length > 0 Then
Select Case header.ToLower
Case "content-type"
wbitem.Type = values(0)
Case "content-length"
wbitem.Length = values(0)
End Select
End If
Next
wbRs.Close()
Catch ex As WebException
wbitem.Type = ex.Message
End Try
///
I hope this helps?

Cor
 
It worked like a charm, thanks. I ended up cutting out everything past the
GetResponse line because if that linee works then the resource exists.

Thanks again,

B.J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top