Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27254

VS 2010 Program stops after error

$
0
0
Hi Guys,

What i'm trying to do is load a list of urls and pages and check for a 200 code, if we receive the "OK" back then perform an action, my code:

Code:

        Try

            '// OPEN FILE
            OpenFileDialog1.Title = "Select URL file..."
            OpenFileDialog1.InitialDirectory = "C:"
            OpenFileDialog1.FileName = "URLs.txt"

            '// IS OK PRESSED?
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

                '// IF SO DISPLAY THE FILE IN THE TEXT BOX
                lbl1.Text = OpenFileDialog1.FileName.ToString()

                '// GET THE LIST READY
                Dim list As New List(Of String)

                '// OPEN THE .TXT FILE WITH THE READER
                Using sReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)

                    '// LOOP OVER EACH LINE
                    Do While sReader.Peek <> -1

                        '// HOLD THE CONTENTS IN A VARIABLE
                        Dim eachURL As String = sReader.ReadLine
                        Dim theStringUrl = eachURL
                        Dim theUri = New Uri(theStringUrl)
                        Dim theDomainTrimmed = theUri.GetLeftPart(UriPartial.Authority)

                        '// [GET] REQUEST
                        Dim GETRequest As HttpWebRequest = TryCast(WebRequest.Create(URL), HttpWebRequest)
                        With GETRequest
                            .Accept = "*/*"
                            .UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
                        End With
                        Dim GETResponse As HttpWebResponse = TryCast(GETRequest.GetResponse(), HttpWebResponse)

                        If (GETResponse.StatusCode = HttpStatusCode.OK) Then

                            Dim GETDataStream As Stream = GETResponse.GetResponseStream()
                            Dim GETReader As New StreamReader(GETDataStream)
                            Dim GETResponseFromServer As String = GETReader.ReadToEnd()

                            If (GETResponseFromServer.Contains("You are not authorized to access this page")) Then
                                MessageBox.Show("Access Denied")
                            End If

                        End If

                    Loop

                    '// CLOSE THE READER ONCE IT'S LOOPED
                    sReader.Close()

                End Using

                MessageBox.Show("Finished.", appInfo, MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If

        Catch ex As Exception

            MessageBox.Show(ex.ToString)

        End Try

The problem is the minute it starts if one url returns an error "403 forbidden" or any other error caught by the try/catch the program throws the error and doesn't continue, i thought the code would execute untill the end of the list of urls but no lol

any help would be appreciated guys :)

cheers

Graham

Viewing all articles
Browse latest Browse all 27254

Latest Images

Trending Articles



Latest Images