Buenas,
Puedes crear una función que verifique eso:
Código vb:
Ver originalPublic Shared Function ExistsRemoteFile(remoteFilename As String) As Boolean
Dim remoteStream As Stream = Nothing
Dim response As WebResponse = Nothing
Dim existFile As Boolean = False
Try
' Create a request for the specified remote file name
Dim request As WebRequest = WebRequest.Create(remoteFilename)
If request IsNot Nothing Then
' Send the request to the server and retrieve the
' WebResponse object
response = request.GetResponse()
If response IsNot Nothing Then
' Once the WebResponse object has been retrieved,
' get the stream object associated with the response's data
remoteStream = response.GetResponseStream()
existFile = remoteStream IsNot Nothing
End If
End If
Catch ex As Exception
existFile = False
Finally
' Close the response and streams objects here
' to make sure they're closed even if an exception
' is thrown at some point
If response IsNot Nothing Then
response.Close()
End If
If remoteStream IsNot Nothing Then
remoteStream.Close()
End If
End Try
Return existFile
End Function
Hay que tener en cuenta que no diferencia entre ficheros y páginas html, es decir, sólo te dice si una dirección web devuelve contenido, pero debería valerte.
Para descargar el fichero puedes usar la siguiente función (sacada de esta página:
http://www.codeguru.com/columns/dotn...se-Classes.htm, la anterior función está basada en esta):
Código vb:
Ver originalPublic Shared Function DownloadFile(remoteFilename As [String], localFilename As [String]) As Integer
' Function will return the number of bytes processed
' to the caller. Initialize to 0 here.
Dim bytesProcessed As Integer = 0
' Assign values to these objects here so that they can
' be referenced in the finally block
Dim remoteStream As Stream = Nothing
Dim localStream As Stream = Nothing
Dim response As WebResponse = Nothing
' Use a try/catch/finally block as both the WebRequest and Stream
' classes throw exceptions upon error
Try
' Create a request for the specified remote file name
Dim request As WebRequest = WebRequest.Create(remoteFilename)
If request IsNot Nothing Then
' Send the request to the server and retrieve the
' WebResponse object
response = request.GetResponse()
If response IsNot Nothing Then
' Once the WebResponse object has been retrieved,
' get the stream object associated with the response's data
remoteStream = response.GetResponseStream()
' Create the local file
localStream = File.Create(localFilename)
' Allocate a 1k buffer
Dim buffer As Byte() = New Byte(1023) {}
Dim bytesRead As Integer
' Simple do/while loop to read from stream until
' no bytes are returned
Do
' Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length)
' Write the data to the local file
localStream.Write(buffer, 0, bytesRead)
' Increment total bytes processed
bytesProcessed += bytesRead
Loop While bytesRead > 0
End If
End If
Catch e As Exception
Console.WriteLine(e.Message)
Finally
' Close the response and streams objects here
' to make sure they're closed even if an exception
' is thrown at some point
If response IsNot Nothing Then
response.Close()
End If
If remoteStream IsNot Nothing Then
remoteStream.Close()
End If
If localStream IsNot Nothing Then
localStream.Close()
End If
End Try
' Return total bytes processed to caller.
Return bytesProcessed
End Function
Un saludo.