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

VS 2010 Looping HTTPWebrequest errors

$
0
0
Hey Guys,

I can't seem to figure out why my code fails:

What i'm doing is looking through a bunch of URLs checking for a string:

Code:

            '// [GET] REQUEST
            Dim GETRequest As HttpWebRequest = TryCast(WebRequest.Create(searchURL), HttpWebRequest)

            With GETRequest
                .Accept = "*/*"
                .UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
            End With

            Try

                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()

                GETDataStream.Close()
                GETResponse.Close()
                GETReader.Close()

                '// STRIP HTML WHITE SPACE //////////////////////////////////////////////////////////////////////////////
                Dim strippedHTML As String = String.Empty
                Dim RegexHTML As New Regex(">[\s]*<")
                strippedHTML = RegexHTML.Replace(GETResponseFromServer, "><")
                '// STRIP HTML WHITE SPACE //////////////////////////////////////////////////////////////////////////////

                'Dim pattern1 As String = "<div class=""floatleft subtext""><a href=""(.*)"">"
                Dim pattern1 As String = "<div class=""title"" id=""title-0""><h2><a href=""(.*)"">"
                Dim m As Match = Regex.Match(strippedHTML, pattern1)
                Dim numbersReplaced As String = String.Empty

                '// IF WE HAVE A SUCCESS/MATCH
                If m.Success Then

                    'MessageBox.Show(m.Groups(1).Value)
                    Dim fishedURL As String() = m.Groups(1).Value.Split(">"c)
                    Dim fishedReplaced As String = fishedURL(0).Replace(" ", "")

                    '// CLEAN THE URL
                    Dim finalURL As String = String.Empty

                    '// REBUILD THE URL
                    Dim finalURLPost As String = theDomain & fishedReplaced

                    '// ADD TO THE LISTBOX
                    listBoxVerify.Items.Add(finalURLPost)

                    '// UPDATE THE LINKS FOUND COUNT
                    lblFoundIncrement.Text = listBoxVerify.Items.Count.ToString

                Else
                    Dim number As Integer
                    number += 1
                    lblFailed.Text = number.ToString
                End If

                'End If

            Catch ex As Exception

                'MessageBox.Show(ex.ToString)

            End Try

        Next

The way the code is above fails after 1 check but if i un-comment

Code:

            Catch ex As Exception

                MessageBox.Show(ex.ToString)

            End Try

The exception error it pops-up in a messagebox i press OK and it continues the routine untill another error appears, then pops up i close etc (This seems to go through the process like this to the very end)

When i put this code in:

Code:

If GETResponse.StatusCode <> HttpStatusCode.OK Then

'// THE CODE HERE

End If

It still fails to execute propely, the only working code is:

Code:

                Dim GETResponse As HttpWebResponse = TryCast(GETRequest.GetResponse(), HttpWebResponse)
                Dim GETDataStream As Stream = GETResponse.GetResponseStream()
                Dim GETReader As New StreamReader(GETDataStream)
                Dim GETResponseFromServer As String = GETReader.ReadToEnd()

                GETDataStream.Close()
                GETResponse.Close()
                GETReader.Close()

                '// STRIP HTML WHITE SPACE //////////////////////////////////////////////////////////////////////////////
                Dim strippedHTML As String = String.Empty
                Dim RegexHTML As New Regex(">[\s]*<")
                strippedHTML = RegexHTML.Replace(GETResponseFromServer, "><")
                '// STRIP HTML WHITE SPACE //////////////////////////////////////////////////////////////////////////////

                'Dim pattern1 As String = "<div class=""floatleft subtext""><a href=""(.*)"">"
                Dim pattern1 As String = "<div class=""title"" id=""title-0""><h2><a href=""(.*)"">"
                Dim m As Match = Regex.Match(strippedHTML, pattern1)
                Dim numbersReplaced As String = String.Empty

                '// IF WE HAVE A SUCCESS/MATCH
                If m.Success Then

                    'MessageBox.Show(m.Groups(1).Value)
                    Dim fishedURL As String() = m.Groups(1).Value.Split(">"c)
                    Dim fishedReplaced As String = fishedURL(0).Replace(" ", "")

                    '// CLEAN THE URL
                    Dim finalURL As String = String.Empty

                    '// REBUILD THE URL
                    Dim finalURLPost As String = theDomain & fishedReplaced

                    '// ADD TO THE LISTBOX
                    listBoxVerify.Items.Add(finalURLPost)

                    '// UPDATE THE LINKS FOUND COUNT
                    lblFoundIncrement.Text = listBoxVerify.Items.Count.ToString

                Else
                    Dim number As Integer
                    number += 1
                    lblFailed.Text = number.ToString
                End If

                'End If

            Catch ex As Exception

                MessageBox.Show(ex.ToString)

            End Try

        Next

But by doing it this way i need to manually close the exception messagebox which can appear over 100 times.

any help would be appreciated guys!

cheers

GrahamLooping httpwebrequiest

Viewing all articles
Browse latest Browse all 27184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>