How is return value generated (1 Viewer)

smiler44

Registered User.
Local time
Today, 09:18
Joined
Jul 15, 2008
Messages
637
I found this code on the internet. It checks that URL's are still valid. Saves me clicking each one of my favourites to see they are still valid.

Code:
Public PageSource As String
Public httpRequest As Object
Dim HL As Hyperlink
 
 
Sub ValidateURLs()
    Dim Cell As String 'Range
    Dim Rng As Range
    Dim RngEnd As Range
    Dim Status As String
    Dim Wks As Worksheet
    
       ' Set Wks = ActiveSheet
       ' Set Rng = Wks.Range("A2")
        Range("B2").Select
start:
        
       ' Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
       ' If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
        
        '    For Each Cell In Rng
        
        
        If ActiveCell.Address = ("$B$1189") Then
           MsgBox ("url's checked exiting")
           Exit Sub
           End If
        
           Cell = ActiveCell.Text
           
           If ActiveCell.Value = "" Or ActiveCell.Value = Empty Then
           ActiveCell.Offset(1, 0).Select 'down 1 across 0
          'MsgBox ("No url in cell, exiting")
          ' Exit Sub
          GoTo start
           End If
          
          
           
                Status = GetURLStatus(Cell)
                If Status <> "200 - OK" Then
                ActiveCell.Offset(0, 1).Select 'across 1
                   'Cell = Status
                   ActiveCell.Value = Status
                   ActiveCell.Offset(0, -1).Select 'back 1
                   Else
                   ActiveCell.Offset(0, 1).Select 'across 1
                   'Cell = Status
                   ActiveCell.Value = Status
                   ActiveCell.Offset(0, -1).Select 'back 1
                End If
         '   Next Cell
             ActiveCell.Offset(1, 0).Select
             GoTo start
        
End Sub
Function GetURLStatus(ByVal URL As String, Optional AllowRedirects As Boolean)
    Const WinHttpRequestOption_EnableRedirects = 6
  
  
        If httpRequest Is Nothing Then
            On Error Resume Next
                Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
                If httpRequest Is Nothing Then
                    Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5")
                End If
            Err.Clear
            On Error GoTo 0
        End If
        ' Control if the URL being queried is allowed to redirect.
          httpRequest.Option(WinHttpRequestOption_EnableRedirects) = AllowRedirects
        ' Clear any pervious web page source information
          PageSource = ""
   
        ' Add protocol if missing
          If InStr(1, URL, "://") = 0 Then
             URL = "http://" & URL
          End If
             ' Launch the HTTP httpRequest synchronously
               On Error Resume Next
                  httpRequest.Open "GET", URL, False
                  If Err.Number <> 0 Then
                   ' Handle connection errors
                     GetURLStatus = Err.Description
                     Err.Clear
                     Exit Function
                  End If
               On Error GoTo 0
           
             ' Send the http httpRequest for server status
               On Error Resume Next
                  httpRequest.Send
                  httpRequest.WaitForResponse
                  If Err.Number <> 0 Then
                   ' Handle server errors
                     PageSource = "Error"
                     GetURLStatus = Err.Description
                     Err.Clear
                  Else
                   ' Show HTTP response info
                     GetURLStatus = httpRequest.Status & " - " & httpRequest.StatusText
                   ' Save the web page text
                     PageSource = httpRequest.ResponseText
                  End If
               On Error GoTo 0
            
End Function

these are the return codes (status) I'm getting but how are they generated

200 - ok
301 - moved permanently
302 - found
403 - forbidden

what is the difference between 200 and 302 .
what does 301 moved permanently mean
what does 403 - forbidden mean


i'm really looking for ok or not found as my results. ok if web page found, not found if web page not found

glassescase
 

jleach

Registered User.
Local time
Today, 04:18
Joined
Jan 4, 2012
Messages
308
Generally speaking:

200 codes (200-299) are "OK"
300 codes are redirects to other pages
400 codes are errors on your part (bad URL, bad format)
500 codes are errors on the web server's part

Keep in mind that it's entirely up to the web server what status code will be reported. Just because there's a (more or less) standard for them doesn't necessarily mean that every web developer makes best use of them.

Also, because there's many variations on the actual numbers and what should be returned, when, for something like your task (as opposed to an API integration task which should make explicit use of codes), you should probably just look for a three digit code and see if the first number is a 2 (OK), 3 (usually OK), 4 or 5 (not OK).

hth
 

smiler44

Registered User.
Local time
Today, 09:18
Joined
Jul 15, 2008
Messages
637
thank you both of you really helpful.
the code is probably a more professional route but all i want is simple, found or not found.

you have been really helpful

thank you for the quick replies

smiler44
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom