I am trying to read in a Web Page, using the code below.
However it keeps failing when it runs the line s = client.OpenRead(remoteUri)
The error I am getting is
The remote name could not be resolved: http://msdn.microsoft.com'
Can anyone offer any suggestions.
Code:Imports System Imports System.IO Imports System.Net Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Specify the URI of the resource to parse. Dim remoteUri As String = "http://msdn.microsoft.com/en-gb/default.aspx" ' Create a WebClient to perform the download. Dim client As New WebClient Console.WriteLine("Downloading {0}", remoteUri) Dim s As System.IO.Stream Try s = client.OpenRead(remoteUri) Catch ex As Exception Debug.Print(ex.Message) Debug.Print(ex.ToString) End ' Halts as no use continuing if we cannot connect to site. End Try Dim r As String Dim sr As System.IO.StreamReader = New System.IO.StreamReader(s, System.Text.Encoding.UTF7, False) r = sr.ReadToEnd() ' Perform the download getting the resource as a string. Dim str As String = client.DownloadString(remoteUri) ' Use a regular expression to extract all fully qualified ' URIs that refer to JPG files. Dim matches As MatchCollection = Regex.Matches(str, "http\S+[^-,;:?]\.jpg") ' Try to download each referenced .JPG files. For Each expMatch As Match In matches For Each grp As Group In expMatch.Groups ' Determine the local filename. Dim downloadedFile As String = grp.Value.Substring(grp.Value.LastIndexOf("/") + 1) Try ' Download and store the file. Console.WriteLine("Downloading {0} to file {1}", grp.Value, downloadedFile) client.DownloadFile(New Uri(grp.Value), downloadedFile) Catch ex As Exception Console.WriteLine("Failed to download {0}", grp.Value) End Try Next Next ' Wait to continue. Console.WriteLine(Environment.NewLine) Console.WriteLine("Main method complete. Press Enter.") Console.ReadLine() End Sub End Module





Reply With Quote