Jump to content

Validate URL especially download link


Go to solution Solved by argumentum,

Recommended Posts

Posted

Hi All,

There is a problem in my code as it will return incorrect return value with download link, inspite other URL return correct

if anyone can help and write code to validate Url

here is my code :

#NoTrayIcon

;$sUrl = "https://githubx.com/ip7z/7zip/releases/download"  ; >>> Return Correct Return Value
$sUrl = "https://github.com/ip7z/7zip/releases/download/26.00/7z2600-x64.exe"  ; >>> Return InCorrect Return Value
;$sUrl = "https://www.google.com" ; >>> Return Correct Return Value

If _CheckUrlAlive($sUrl) Then
    MsgBox(64+262144, "Success", "URL is alive")
Else
    MsgBox(16+262144, "Error", "URL is dead or inaccessible")
EndIf

Func _CheckUrlAlive($sUrl)
    Local $ONERRORHANDLER = ObjEvent("AutoIt.Error", "_TIMED_ERRFUNC")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If Not IsObj($oHTTP) Then Return False

    $oHTTP.Open("HEAD", $sUrl, False) ; Use HEAD to avoid downloading the file
    $oHTTP.Send()

    Local $iStatus = $oHTTP.Status
    Return $iStatus
    ; 200 = OK, 301/302 = Redirect (still alive)
    Return ($iStatus >= 200 And $iStatus <= 400)
EndFunc
Func _TIMED_ERRFUNC($ONERRORHANDLER)
    ConsoleWrite($ONERRORHANDLER.description)
EndFunc   ;==>_TIME_ERRFUNC

 

Thanks in advance

  • Solution
Posted
#include <MsgBoxConstants.au3>

Global $sUrl = "https://github.com/ip7z/7zip/releases/download/26.00/7z2600-x64.exe"

If _CheckUrlAlive($sUrl) Then
    MsgBox($MB_ICONINFORMATION + $MB_SYSTEMMODAL, "Success", "URL is alive: " & @extended)
Else
    MsgBox($MB_ICONERROR + $MB_SYSTEMMODAL, "Error", "URL is dead or inaccessible")
EndIf

Func _CheckUrlAlive($sUrl)
    Local $oMyError = ObjEvent("AutoIt.Error", "_COMErrorFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If Not IsObj($oHTTP) Then Return False

    ; Enable redirects (Option 6) and HTTPS-to-HTTP transitions (Option 12)
    $oHTTP.Option(6) = True
    $oHTTP.Option(12) = True

    $oHTTP.Open("HEAD", $sUrl, False)
    $oHTTP.Send()

    If @error Then Return False

    Local $iStatus = $oHTTP.Status
    Return SetError(0, $iStatus, ($iStatus >= 200 And $iStatus < 400))
EndFunc

Func _COMErrorFunc() ; This function runs automatically if a COM error occurs
    ConsoleWrite("COM Error Intercepted!" & @CRLF)
    Return
EndFunc

🤔

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

..or just get the file ?
 

#include <MsgBoxConstants.au3>

Local $sUrl = "https://github.com/ip7z/7zip/releases/download/26.00/7z2600-x64.exe"
Local $sSavePath = @ScriptDir & "\7z2600-x64.exe"

If _DownloadBinaryFile($sUrl, $sSavePath) Then
    MsgBox($MB_ICONINFORMATION, "Success", "File downloaded successfully to:" & @CRLF & $sSavePath)
Else
    MsgBox($MB_ICONERROR, "Error", "Failed to download the file.")
EndIf

Func _DownloadBinaryFile($sUrl, $sFileName)
    Local $oMyError = ObjEvent("AutoIt.Error", "_COMErrorFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If Not IsObj($oHTTP) Then Return False

    ; Enable redirects for GitHub CDN
    $oHTTP.Option(6) = True
    $oHTTP.Option(12) = True

    $oHTTP.Open("GET", $sUrl, False)
    $oHTTP.Send()

    If @error Then Return False

    Local $iStatus = $oHTTP.Status
    If $iStatus >= 200 And $iStatus < 400 Then
        ; Use ResponseBody for binary files like .exe
        Local $vBinaryData = $oHTTP.ResponseBody

        ; Open file in Binary mode (16) + Overwrite (2) = 18
        Local $hFile = FileOpen($sFileName, 18)
        If $hFile = -1 Then Return False

        FileWrite($hFile, $vBinaryData)
        FileClose($hFile)
        Return True
    EndIf

    Return False
EndFunc

Func _COMErrorFunc()
    Return ; Ignore errors to let script handle them via @error
EndFunc

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted
7 minutes ago, argumentum said:
#include <MsgBoxConstants.au3>

Global $sUrl = "https://github.com/ip7z/7zip/releases/download/26.00/7z2600-x64.exe"

If _CheckUrlAlive($sUrl) Then
    MsgBox($MB_ICONINFORMATION + $MB_SYSTEMMODAL, "Success", "URL is alive: " & @extended)
Else
    MsgBox($MB_ICONERROR + $MB_SYSTEMMODAL, "Error", "URL is dead or inaccessible")
EndIf

Func _CheckUrlAlive($sUrl)
    Local $oMyError = ObjEvent("AutoIt.Error", "_COMErrorFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If Not IsObj($oHTTP) Then Return False

    ; Enable redirects (Option 6) and HTTPS-to-HTTP transitions (Option 12)
    $oHTTP.Option(6) = True
    $oHTTP.Option(12) = True

    $oHTTP.Open("HEAD", $sUrl, False)
    $oHTTP.Send()

    If @error Then Return False

    Local $iStatus = $oHTTP.Status
    Return SetError(0, $iStatus, ($iStatus >= 200 And $iStatus < 400))
EndFunc

Func _COMErrorFunc() ; This function runs automatically if a COM error occurs
    ConsoleWrite("COM Error Intercepted!" & @CRLF)
    Return
EndFunc

🤔

Thanks for reply

but this code return incorrect value as the download link is Ok & Alive working

this code return : Error : Dead or inaccessible URL

Posted (edited)
6 minutes ago, argumentum said:

..or just get the file ?
 

#include <MsgBoxConstants.au3>

Local $sUrl = "https://github.com/ip7z/7zip/releases/download/26.00/7z2600-x64.exe"
Local $sSavePath = @ScriptDir & "\7z2600-x64.exe"

If _DownloadBinaryFile($sUrl, $sSavePath) Then
    MsgBox($MB_ICONINFORMATION, "Success", "File downloaded successfully to:" & @CRLF & $sSavePath)
Else
    MsgBox($MB_ICONERROR, "Error", "Failed to download the file.")
EndIf

Func _DownloadBinaryFile($sUrl, $sFileName)
    Local $oMyError = ObjEvent("AutoIt.Error", "_COMErrorFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If Not IsObj($oHTTP) Then Return False

    ; Enable redirects for GitHub CDN
    $oHTTP.Option(6) = True
    $oHTTP.Option(12) = True

    $oHTTP.Open("GET", $sUrl, False)
    $oHTTP.Send()

    If @error Then Return False

    Local $iStatus = $oHTTP.Status
    If $iStatus >= 200 And $iStatus < 400 Then
        ; Use ResponseBody for binary files like .exe
        Local $vBinaryData = $oHTTP.ResponseBody

        ; Open file in Binary mode (16) + Overwrite (2) = 18
        Local $hFile = FileOpen($sFileName, 18)
        If $hFile = -1 Then Return False

        FileWrite($hFile, $vBinaryData)
        FileClose($hFile)
        Return True
    EndIf

    Return False
EndFunc

Func _COMErrorFunc()
    Return ; Ignore errors to let script handle them via @error
EndFunc

 

This code gives an Error message : Failed to download the file

so not working with me

Edited by Davidyese
Posted (edited)
7 minutes ago, argumentum said:

..worked for me. Are you blocked in the area were you live ?

Saudi Arabia

i can use internet settings to adjust

so what is the countery that could help the code to work ???

Edited by Davidyese
Posted (edited)

..and a hash checker for github:

#include <MsgBoxConstants.au3>

Local $sUrl = "https://github.com/ip7z/7zip/releases/download/26.00/7z2600-x64.exe"
Local $sHash = _GetGitHubAssetHash($sUrl)

If $sHash Then
    ConsoleWrite(@CRLF & $sHash & @CRLF)
    MsgBox($MB_ICONINFORMATION, "Found Hash", "Official SHA-256 Digest:" & @CRLF & $sHash)
Else
    MsgBox($MB_ICONERROR, "Error", "Could not retrieve hash from GitHub API.")
EndIf

Func _GetGitHubAssetHash($sUrl)
    ; Parse the URL to get owner, repo, and tag
    Local $aMatch = StringRegExp($sUrl, "github\.com/([^/]+)/([^/]+)/releases/download/([^/]+)/([^/]+)", 3)
    If UBound($aMatch) < 4 Then Return ""

    Local $sOwner = $aMatch[0], $sRepo = $aMatch[1], $sTag = $aMatch[2], $sFile = $aMatch[3]

    ; Build the API URL for this release
    Local $sApiUrl = "https://api.github.com/repos/" & $sOwner & "/" & $sRepo & "/releases/tags/" & $sTag

    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $oHTTP.Open("GET", $sApiUrl, False)

    ; GitHub API requires a User-Agent header
    $oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)") ; Use a real browser string ?
    $oHTTP.Send()

    If @error Then
        ConsoleWrite("Network Error: Could not connect to GitHub. Check your ISP/VPN." & @CRLF)
        Return SetError(1, @ScriptLineNumber, "")
    EndIf

    If $oHTTP.Status = 403 Then
        ConsoleWrite("Rate Limit Exceeded! Check: https://github.com" & @CRLF)
        Return SetError(2, @ScriptLineNumber, "")
    EndIf

    If $oHTTP.Status <> 200 Then Return SetError(3, @ScriptLineNumber, "")

    ; Use Regex to find the 'digest' for our specific filename in the JSON response
    ; Recent GitHub releases include a "digest" field for each asset
    Local $sResponse = $oHTTP.ResponseText
    Local $sPattern = '"name"\s*:\s*"' & $sFile & '".*?"digest"\s*:\s*"([^"]+)"'
    Local $aHashMatch = StringRegExp($sResponse, $sPattern, 3)

    If UBound($aHashMatch) > 0 Then Return $aHashMatch[0]
    Return SetError(4, @ScriptLineNumber, "")
EndFunc

 

Edited by argumentum
better

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted
10 minutes ago, argumentum said:

Am in the US. But if a VPN don't work, what @Nine said is true too.

GitHub API has a rate limit (usually 60 requests per hour for unauthenticated users).

Thanks my dear for your help

maybe my countery has some restrictions to some websites

but it's Okay, i found another simpler solution to check the mentioned links before especially : download links

here is the code but unfortunately it takes long time with some dead website like this dead link :  "https://githubx.com/ip7z/7zip/releases/download"

the new simple code work with me perfectly 100% and i hope it will help anyone else

#NoTrayIcon

Local $TestURL = "https://githubx.com/ip7z/7zip/releases/download"
;Local $TestURL = "https://github.com/ip7z/7zip/releases/download/26.00/7z2600-x64.exe"
;Local $TestURL = "https://www.google.com"
;Local $TestURL = "https://www.googlarsds.com"

Local $Timer = TimerInit()
If _UrlAlive($TestURL) Then
    Local $Diff = Int(TimerDiff($Timer))
    MsgBox(64+262144, "Success", "URL is alive" & "  [ " & Round($Diff / 1000, 3) & " ]" )
Else
    Local $Diff = Int(TimerDiff($Timer))
    MsgBox(16+262144, "Error", "URL is dead or inaccessible" & "  [ " & Round($Diff / 1000, 3) & " ]" )
EndIf
Exit

Func _UrlAlive($sUrl)
    Return Not (InetRead($sUrl, 1) == "") ; 1 = Force reload from remote
EndFunc

 

Posted (edited)
On 4/13/2026 at 8:40 PM, argumentum said:
#include <MsgBoxConstants.au3>

Global $sUrl = "https://github.com/ip7z/7zip/releases/download/26.00/7z2600-x64.exe"

If _CheckUrlAlive($sUrl) Then
    MsgBox($MB_ICONINFORMATION + $MB_SYSTEMMODAL, "Success", "URL is alive: " & @extended)
Else
    MsgBox($MB_ICONERROR + $MB_SYSTEMMODAL, "Error", "URL is dead or inaccessible")
EndIf

Func _CheckUrlAlive($sUrl)
    Local $oMyError = ObjEvent("AutoIt.Error", "_COMErrorFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If Not IsObj($oHTTP) Then Return False

    ; Enable redirects (Option 6) and HTTPS-to-HTTP transitions (Option 12)
    $oHTTP.Option(6) = True
    $oHTTP.Option(12) = True

    $oHTTP.Open("HEAD", $sUrl, False)
    $oHTTP.Send()

    If @error Then Return False

    Local $iStatus = $oHTTP.Status
    Return SetError(0, $iStatus, ($iStatus >= 200 And $iStatus < 400))
EndFunc

Func _COMErrorFunc() ; This function runs automatically if a COM error occurs
    ConsoleWrite("COM Error Intercepted!" & @CRLF)
    Return
EndFunc

 

..or just get the file ?
 

#include <MsgBoxConstants.au3>

Local $sUrl = "https://github.com/ip7z/7zip/releases/download/26.00/7z2600-x64.exe"
Local $sSavePath = @ScriptDir & "\7z2600-x64.exe"

If _DownloadBinaryFile($sUrl, $sSavePath) Then
    MsgBox($MB_ICONINFORMATION, "Success", "File downloaded successfully to:" & @CRLF & $sSavePath)
Else
    MsgBox($MB_ICONERROR, "Error", "Failed to download the file.")
EndIf

Func _DownloadBinaryFile($sUrl, $sFileName)
    Local $oMyError = ObjEvent("AutoIt.Error", "_COMErrorFunc")
    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If Not IsObj($oHTTP) Then Return False

    ; Enable redirects for GitHub CDN
    $oHTTP.Option(6) = True
    $oHTTP.Option(12) = True

    $oHTTP.Open("GET", $sUrl, False)
    $oHTTP.Send()

    If @error Then Return False

    Local $iStatus = $oHTTP.Status
    If $iStatus >= 200 And $iStatus < 400 Then
        ; Use ResponseBody for binary files like .exe
        Local $vBinaryData = $oHTTP.ResponseBody

        ; Open file in Binary mode (16) + Overwrite (2) = 18
        Local $hFile = FileOpen($sFileName, 18)
        If $hFile = -1 Then Return False

        FileWrite($hFile, $vBinaryData)
        FileClose($hFile)
        Return True
    EndIf

    Return False
EndFunc

Func _COMErrorFunc()
    Return ; Ignore errors to let script handle them via @error
EndFunc

 

It's Working with me now after correcting some proxy internet settings

Thanks alot my dear argumentum

Edited by Davidyese

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...