Daniel Padron wrote:
<snip>
> My goal is create a program that will get feeded a url that links direcly
> to a file [ex.http://www.example.com/video.avi] the program will then
> determine if the file is there or not.
<snip>
It depends on what you really want with the "file" (not always the
link points really to the file. Sometimes the server performs some
processing before actually retrieving the "file", and may actually
return something completely different).
If you just want to check that the link is valid, then you may
consider using the System.Net.HttpRequest class:
<code>
'This must be at the file level:
Imports Net = System.Net
'...
'...
'...
'This would be in a method:
' Initializes the request
Dim Link As String = "http://www.example.com/video.avi"
Dim Req As Net.HttpWebRequest = _
CType(Net.WebRequest.Create(Link), Net.HttpWebRequest)
Req.Credentials = Net.CredentialCache.DefaultCredentials
'Prepares to probe the link
Dim Res As Net.HttpWebResponse = Nothing
Dim ValidLink As Boolean
Try
'Tries to get the an ok response from the server
Res = CType(Req.GetResponse(), Net.HttpWebResponse)
ValidLink = True
Catch Ex As Net.WebException
Finally
If Res IsNot Nothing Then Res.Close()
End Try
</code>
I guess you'll have no trouble following the code. What this code does
is to set a Boolean flag (ValidLink) if the link is valid or not. It
won't be valid if the server can't be located or if the server returns
an error code (which will be the case if the file is not there).
Now, if what you really want is to *download* the link's content, then
an easier solution would be to use the System.Net.WebClient class:
<code>
'Again, this must be at the file level:
Imports Net = System.Net
'...
'...
'...
'This would be in a method:
Dim Link As String = "http://www.example.com/video.avi"
Dim Target As String = "C:\video.avi"
Dim Downloaded As Boolean
Dim W As New Net.WebClient
Try
W.DownloadFile(Link, Target)
Downloaded = True
Catch Ex As Net.WebException
End Try
</code>
The code above will download the file returned by the specified link
and save its contents in the "C:\video.avi" file (a terrible choice of
location, I admit, but this is just an example. I believe you'd put
the download in a more apropriate folder). If the link is valid and
the file was downloaded, the "Downloaded" flag will be set to True.
HTH.
Regards,
Branco.