Sign in to follow this  
Followers 0

Is there a way to check if a URL exists without calling it?

16 posts in this topic

Posted (edited)

For instance, if I wanted to know if www.AnySite.com/AnyFile.html exists, is there a way to do this? Sort of like:

If www.AnySite.com/AnyFile.html exists then

else

endif

Edited by LovinItAll

Share this post


Link to post
Share on other sites



Posted (edited)

For instance, if I wanted to know if www.AnySite.com/AnyFile.html exists, is there a way to do this? Sort of like:

If www.AnySite.com/AnyFile.html exists then

else

endif

Sure -- this should work...

;Check existance of a URL
;adapted to AutoIt from http://www.developerfusion.co.uk/show/1605/
;by DaleHohm

;Timeout values in milliseconds
$ResolveTimeout = 500
$ConnectTimeout = 500
$SendTimeout = 500
$ReceiveTimeout = 500

$oHttpRequest = ObjCreate("MSXML2.ServerXMLHTTP")
If $oHttpRequest = 0 Then 
    ConsoleWrite("Error creating Object $oHttpRequest.  " & _
                        "You must have 3.0 or later of the MSXML library to use this code" & @CR)
EndIf
$sTestUrl = "http://www.microsoft.com/nonexistingpage.html"

With $oHttpRequest
   .SetTimeouts ($ResolveTimeout, $ConnectTimeout, $SendTimeout, $ReceiveTimeout)
   .Open ("GET", $sTestUrl)
   .Send
   Select 
      Case .Status = 200;No problem!
        ConsoleWrite($sTestUrl & " is a valid, available URL" & @CR)
      Case .Status = 404;Not found
        ConsoleWrite($sTestUrl & " could not be found (404 Error)" & @CR)
      Case Else;Some other problem
        ConsoleWrite("An unexpected HTTP Status value was returned: " & .Status & @CR)
   EndSelect
EndWith

$oHttpRequest = ""

Enjoy,

Dale

Edit: Added check object creation and caveat about MSXML 3.0

Edited by DaleHohm

Share this post


Link to post
Share on other sites

Posted

@DaleHohm,

I get:

>Running: (3.1.1.110):C:\Program Files\AutoIt3\beta\autoit3.exe "c:\Temp\SciTE-temp.au3"

C:\Temp\SciTE-temp.au3 (13) : ==> The requested action with this object has failed.:

.Send

.Send^ ERROR

>AutoIT3.exe ended.

with that code...

Share this post


Link to post
Share on other sites

Posted

Works fine for me...

Try this version to see what the problem with the Com object is..

;Check existance of a URL
;adapted to AutoIt from http://www.developerfusion.co.uk/show/1605/
;by DaleHohm

;Timeout values in milliseconds
$ResolveTimeout = 500
$ConnectTimeout = 500
$SendTimeout = 500
$ReceiveTimeout = 500
;
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$oHttpRequest = ObjCreate("MSXML2.ServerXMLHTTP")
$sTestUrl = "http://www.microsoft.com/nonexistingpage.html"
;
With $oHttpRequest
   .SetTimeouts ($ResolveTimeout, $ConnectTimeout, $SendTimeout, $ReceiveTimeout)
   .Open ("GET", $sTestUrl)
   .Send
   Select 
      Case .Status = 200;No problem!
        ConsoleWrite($sTestUrl & " is a valid, available URL" & @CR)
      Case .Status = 404;Not found
        ConsoleWrite($sTestUrl & " could not be found (404 Error)" & @CR)
      Case Else;Some other problem
        ConsoleWrite("An unexpected HTTP Status value was returned: " & .Status & @CR)
   EndSelect
EndWith
;
$oHttpRequest = ""
;
; Com Error Handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description,3)
    ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF) 
    SetError(1); something to check for when this function returns
    Return
EndFunc  ;==>MyErrFunc

Share this post


Link to post
Share on other sites

Posted

@DaleHohm,

I get:with that code...

Sorry, there is a requirement for MSXML 3.0. JdeB's error handler is always a good idea (when you aren't being lazy like I was :o ). I've also added a check to my original code that checks the status of the ObjCreate code.

Dale

Share this post


Link to post
Share on other sites

Posted

JdeB,

>Running: (3.1.1.110):C:\Program Files\AutoIt3\beta\autoit3.exe "c:\Temp\SciTE-temp.au3"

C:\Temp\SciTE-temp.au3 (34) : ==> Variable used without being declared.:

$oMyRet[0]= $HexNumber

^ ERROR

>AutoIT3.exe ended.

Share this post


Link to post
Share on other sites

Posted

Dale - Thank you so much! You have come through for me in a clutch way -- again!

Regards ~ Lee

Share this post


Link to post
Share on other sites

Posted (edited)

Edit: Added check object creation and caveat about MSXML 3.0

If $oHttpRequest = 0 Then 
    ConsoleWrite("Error creating Object $oHttpRequest.  " & _
                        "You must have 3.0 or later of the MSXML library to use this code" & @CR)
EndIf
I never see that caveat in the console output:

>"C:\Program Files\AutoIt3\SciTe\CompileAU3\CompileAU3.exe" /run /beta /ErrorStdOut /in "c:\Temp\SciTE-temp.au3" /autoit3dir "C:\Program Files\AutoIt3\beta" /UserParams

>Running: (3.1.1.110):C:\Program Files\AutoIt3\beta\autoit3.exe "c:\Temp\SciTE-temp.au3"

C:\Temp\SciTE-temp.au3 (21) : ==> The requested action with this object has failed.:

.Send

.Send^ ERROR

>AutoIT3.exe ended.

>Exit code: 0 Time: 1.541

Edit: So I guess that I have MSXML 3.0 installed and I still get the error above.

http://www.microsoft.com/downloads/details...&displaylang=en

Edited by herewasplato

Share this post


Link to post
Share on other sites

Posted (edited)

JdeB,

I am sorry, copied it from another script and forgot about the array..

try this version of the ComErrorHandler Func

; Com Error Handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & StringStripWS($oMyError.description,3) & @LF) 
    SetError(1); something to check for when this function returns
    Return
EndFunc  ;==>MyErrFunc
Edited by JdeB

Share this post


Link to post
Share on other sites

Posted (edited)

I am sorry, copied it from another script and forgot about the array..

No problem. This is the output now:

>Running: (3.1.1.110):C:\Program Files\AutoIt3\beta\autoit3.exe "c:\Temp\SciTE-temp.au3"

### COM Error ! Number: 80020009 ScriptLine: 18 Description:The server name or address could not be resolved

### COM Error ! Number: 80020009 ScriptLine: 22 Description:The data necessary to complete this operation is not yet available.

An unexpected HTTP Status value was returned:

### COM Error ! Number: 80020009 ScriptLine: 25 Description:The data necessary to complete this operation is not yet available.

### COM Error ! Number: 80020009 ScriptLine: 25 Description:The data necessary to complete this operation is not yet available.

>AutoIT3.exe ended.

Edit: Tested without a proxy server to the net - works fine.

Returns (404 Error) as expected.

Edited by herewasplato

Share this post


Link to post
Share on other sites

Posted

????

Ping ( address or hostname [, timeout] )

When the function fails (returns 0) @error contains extended information:

1 = Host is offline

2 = Host is unreachable

3 = Bad destination

4 = Other errors

????

8)

Share this post


Link to post
Share on other sites

Posted (edited)

No problem. This is the output now:

Edit: Tested without a proxy server to the net - works fine.

Returns (404 Error) as expected.

Here is a very good page about ServerXMLHTTP: Frequently asked questions about ServerXMLHTTP. One of the things it hilights is that ServerXMLHTTP does not do automatic proxy discovery and that you may have to run proxycfg.exe first... this would explain your trouble I believe.

There is an older component called XMLHTTP that is also discussed in that article that does do automatic proxy detection. I believe it could also be used to do this discovery, but from my experience may need some additional error handling when used in AutoIt. I have not done any testing of this however.

Dale

Edit: fixed hyperlink

Edited by DaleHohm

Share this post


Link to post
Share on other sites

Posted

????

Ping ( address or hostname [, timeout] )

When the function fails (returns 0) @error contains extended information:

1 = Host is offline

2 = Host is unreachable

3 = Bad destination

4 = Other errors

????

8)

I tried this originally, but Ping will not detect the existence of folders within a URL. For example:

ping("autoitscript.com") - will return the ping time

ping("autoitscript.com/AnyFolder") will always return 0, whether AnyFolder exists or not.

Share this post


Link to post
Share on other sites

Posted

Here is a very good page about ServerXMLHTTP: Frequently asked questions about ServerXMLHTTP. One of the things it hilights is that ServerXMLHTTP does not do automatic proxy discovery and that you may have to run proxycfg.exe first... this would explain your trouble I believe.

There is an older component called XMLHTTP that is also discussed in that article that does do automatic proxy detection. I believe it could also be used to do this discovery, but from my experience may need some additional error handling when used in AutoIt. I have not done any testing of this however.

Dale

Edit: fixed hyperlink

Is there anyway to connect to a proxy ? - I would have server name or IP, username, password and would have the port numbeer.

Or has this question already been asked and answered?

Thanks

Share this post


Link to post
Share on other sites

Posted

Is there anyway to connect to a proxy ? - I would have server name or IP, username, password and would have the port numbeer.

Or has this question already been asked and answered?

Thanks

From above: "you may have to run proxycfg.exe first" -- see the FAQ referenced. Or use XMLHTTPRequest that does auto proxy discovery.

Dale

Share this post


Link to post
Share on other sites

Posted (edited)

From above: "you may have to run proxycfg.exe first" -- see the FAQ referenced. Or use XMLHTTPRequest that does auto proxy discovery.

Dale

I read the information and it appears that you can grab the info from the registry for HKCU - is this correct? If that is the case I can write the info from there...to the HKLM without a reboot? Or would it be better to call this application from within the script, but then I would have to include it with the script as I am sure not everyone would have the proxycfg.exe installed - I had to download it. The reason for all this is I would like to be able to run programs like the ccweather.au3 at my work which has an proxy server (ISA).

Microsoft Windows XP [Version 5.1.2600]

© Copyright 1985-2001 Microsoft Corp.

C:\programming>proxycfg

Current WinHTTP proxy settings under

HKEY_LOCAL_MACHINE\

SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections\

WinHttpSettings :

Flags = PROXY_TYPE_DIRECT

Proxy Server = -not set-

Bypass List = -not set-

C:\programming>proxycfg -?

WinHTTP Proxy Configuration Tool

usage:

proxycfg -? : to view help information

proxycfg : to view current winhttp proxy settings (in HKLM)

proxycfg [-d] [-p <server-name> [<bypass-list>]]

-d : set PROXY_TYPE_DIRECT

-p : set PROXY_TYPE_PROXY, proxy server, and optional bypass list

proxycfg -u : to set winhttp proxy settings

from current user's manual setting (in HKCU)

Edited by nitekram

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.