check if url exists (1 Viewer)

cpampas

Registered User.
Local time
Today, 03:18
Joined
Jul 23, 2012
Messages
218
Hello,
I am willing to check if a specific webpage exist, and for that i am trying the function I got in one of previous posts

Code:
Function URLExists(url As String) As Boolean
    Dim Request As Object
    Dim ff As Integer
    Dim rc As Variant

    On Error GoTo EndNow
    Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
  
    With Request
      .Open "GET", url, False
      .send
      rc = .statusText
    End With
    Set Request = Nothing
    If rc = "OK" Then URLExists = True
  
    Exit Function
EndNow:
End Function

and yet, when I run de function to check the following page, it returns TRUE when in fact that page does not exist. why would that be ?

Code:
Dim str As String
str = "https://autoline.info/-/sale/mini-excavators/CATERPILLAR-303-5ECR--21030603381891176300"
MsgBox (URLExists(str))

Thanks for helping
 

Isaac

Lifelong Learner
Local time
Today, 03:18
Joined
Mar 14, 2017
Messages
8,738
The link you've posted does work for me (exists) ?

In fact, the function you've posted returns False for http://www.google.com

Comment out the line: On Error GoTo EndNow
(which basically just prohibits you from knowing if the function is even working, by reacting to any error by simply exiting the function, and retaining the boolean's default False value), and you might see that the function is timing out (at least it is for me)......thus falsely returning False, but that False return isn't really meaningful.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:18
Joined
Oct 29, 2018
Messages
21,358
and yet, when I run the following page, it returns when in fact that page does not exist. why would that be ?
Since you were able to post a link to it, it means it does exist.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:18
Joined
Oct 29, 2018
Messages
21,358
I am getting different results here:
http://www.google.com returns true , wich is correct
my above link returns true, wich is not correct, since after attempting to load the link I am redirected to another url (https://autoline.info/-/mini-excavators--c166), wich is different from the one i want to check.
In that case, you might want to check the request's status.

 

cpampas

Registered User.
Local time
Today, 03:18
Joined
Jul 23, 2012
Messages
218
Successful responses (200–299)
'Redirects (300–399)

The request's status returns the code 200 wich is a successfull reponse, shouldnt I get a code between 300-399 ? after all the page is not there , and it´s being redirected to another one
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:18
Joined
Oct 29, 2018
Messages
21,358
Successful responses (200–299)
'Redirects (300–399)

The request's status returns the code 200 wich is a successfull reponse, shouldnt I get a code between 300-399 ? after all the page is not there , and it´s being redirected to another one
That all depends on how the web resource is handling your request. Some might tell you exactly what they are doing, while others might decide to just tell you if your request failed or not.
 

cpampas

Registered User.
Local time
Today, 03:18
Joined
Jul 23, 2012
Messages
218
I ended up changing the function, and now I get the status code as 301 ( redirected page), therefor non existing

Code:
Function URLExists(url As String) As Boolean
    Dim request As Object
    Dim ff As Integer
    Dim rc As Variant
 
    Set request = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    With request
       .Option(WinHttpRequestOption_EnableRedirects) = False
       .Open "HEAD", url, False
       .send
       rc = .status
    End With
    Set request = Nothing
    If rc = 200 Then URLExists = True
    
    Exit Function
EndNow:
End Function

theDBGuy, thanks for pointing me to the right direction
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:18
Joined
Oct 29, 2018
Messages
21,358
I ended up changing the function, and now I get the status code as 301 ( redirected page), therefor non existing

Code:
Function URLExists(url As String) As Boolean
    Dim request As Object
    Dim ff As Integer
    Dim rc As Variant

    Set request = CreateObject("WinHttp.WinHttpRequest.5.1")
   
    With request
       .Option(WinHttpRequestOption_EnableRedirects) = False
       .Open "HEAD", url, False
       .send
       rc = .status
    End With
    Set request = Nothing
    If rc = 200 Then URLExists = True
   
    Exit Function
EndNow:
End Function

theDBGuy, thanks for pointing me to the right direction
Hi. You're welcome. Glad to hear you got it sorted out. Good luck with your project.
 

Users who are viewing this thread

Top Bottom