VB.net Program to check if a file exists at a particular URL and download the content

Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 1918
Imports System.Net

Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())
Dim fileLocation As String = "http://TestWebAPI/SampleText/sample.txt"
Dim serverResponse = GetServerResponse(fileLocation)
Console.WriteLine("URL Validity : {0} " & vbLf & " Server Response : {1}", serverResponse.Item1, serverResponse.Item2)
End Sub

Private Shared Function GetServerResponse(fileLocation As String) As Tuple(Of Boolean, String)
Dim isValidURL As Boolean = True
Dim data As String = String.Empty
Using client As New WebClient()
data = client.DownloadString(fileLocation)
Catch weX As WebException
isValidURL = Not isValidURL
data = weX.Message
End Try
End Using
Return Tuple.Create(Of Boolean, String)(isValidURL, data)
End Function
End Class
End Namespace

Comments or Responses

Login to post response