Jump to content

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


Recommended Posts

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

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

@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...

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@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

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

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

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

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

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

????

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.

Link to comment
Share on other sites

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

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

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

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

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

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...