Sign in to follow this  
Followers 0

How to ping long URL not just the root path

23 posts in this topic

Posted

Pinging works fine for this short URL:

Local $var = Ping("www.ibiblio.org", 250)
If $var Then; also possible:  If @error = 0 Then ...
  MsgBox(0, "Status", "Online, roundtrip was:" & $var)
Else
  MsgBox(0, "Status", "An error occured with number: " & @error)
EndIf

But not for that:

Local $var = Ping("www.ibiblio.org/pub/multimedia/mutopia/BachJS/BWV16/Cantata_16_no_5/", 250)
If $var Then; also possible:  If @error = 0 Then ...
  MsgBox(0, "Status", "Online, roundtrip was:" & $var)
Else
  MsgBox(0, "Status", "An error occured with number: " & @error)
EndIf

Is it possible to ping full path to directories?

Thanks

Share this post


Link to post
Share on other sites



Posted

No, you can't ping a folder, you have to ping the server, which is the part between the // and the / in the URL.

Share this post


Link to post
Share on other sites

Posted (edited)

Thanks brewman, is there any function to check if the long URL/folders are online or if the URL is valid or not?

I used this code but not error pops up with unvalid URL.

#include <IE.au3>
$oIE = _IECreate("www.autoitscriptnoexist.com/nonexistantfolder", 0)

How to check if a long path URL is valid?

Edited by xuzo

Share this post


Link to post
Share on other sites

Posted

Thanks brewman, is there any function to check if the long URL/folders are online or if the URL is valid or not?

I used this code but not error pops up with unvalid URL.

#include <IE.au3>
$oIE = _IECreate("www.autoitscriptnoexist.com/nonexistantfolder", 0)

How to check if a long path URL is valid?

What you are asking is not possible; servers and resources simply do not work that way.

PING is to determine the packet loss and speed of a server. An example of a server is autoitscript.com or ibiblio.org.

Trying to ping ibiblio.org/pub/multimedia/mutopia/BachJS/BWV16/Cantata_16_no_5/ is not valid.

What you are trying to do, rather, is determine if a resource on a server is accessible. You are not trying to determine if the server itself is reachable thus using ping is not valid.

Instead, you should make a request to the server for the URL ibiblio.org/pub/multimedia/mutopia/BachJS/BWV16/Cantata_16_no_5/ or whatever. It will return a 200 OK message (lookup HTML headers if you do not know what that means) if the resource is available. Anything else is a special-case or error, and depends on the code. For example, a 404 indicates the resource does not exist.

Share this post


Link to post
Share on other sites

Posted

I would first do a Ping to check if the server is up and running and then do a

$iSize = InetGetSize("URL", 1)
This returns just the size of the html page and doesn't transfer the whole content. If it doesn't exist @error is set.

Share this post


Link to post
Share on other sites

Posted (edited)

Thank you to all you guys for the expert advice, but still no clue!....

I tried this:

#include <IE.au3>
$oIE = _IECreate("www.autoitscriptnoexist.com/nonexistantfolder", 0)
$iSize = InetGetSize("www.autoitscriptnoexist.com/nonexistantfolder", 1)

I'm aware of 404, 503 and 500 errors but don't know how to make AutoIT "read the error", I have yet to learn how to do an if command that can read:

"if the text on the page says 404" then do "this"

I'm trying to find a way for the sript to tell me if the page is online or not, if the URL exists or not. And then choose the next step in the script.

Edited by xuzo

Share this post


Link to post
Share on other sites

Posted

Error codes 404 etc. are HTTP error codes. To retrieve this codes you need to use the written by trancexx.

Share this post


Link to post
Share on other sites

Posted (edited)

Thank you guys!

Ok sounds good, but how could I do this:

Instead, you should make a request to the server for the URL ibiblio.org/pub/multimedia/mutopia/BachJS/BWV16/Cantata_16_no_5/ or whatever. It will return a 200 OK message (lookup HTML headers if you do not know what that means) if the resource is available. Anything else is a special-case or error, and depends on the code. For example, a 404 indicates the resource does not exist.

What is the code to do this:

"request to the server for the URL"

Edited by xuzo

Share this post


Link to post
Share on other sites

Posted

Sorry, haven't done it myself. I fear you will have to dig through the thread and search for the HTTP requests you need to send yourself.

Share this post


Link to post
Share on other sites

Posted (edited)

How about looking for text on the page, if the text is there, it's ok, if the text isn't there stop or do something else?

In my particular task the text:

"Index of ftp://ibiblio.org"

is on all the pages that are online, if that text is not on the page means the page is down.

So how could I set up:

1. Go to URL

2. Check on the page to see if "Index of ftp://ibiblio.org" is there

3. If the text is diplayed do nothing and move to next step

4. If text is not there pop up an error message

I'm starting to get a grasp on if commands but not sure how to fetch the text on an HTML document either from visible view or source code.

Thanks

Edited by xuzo

Share this post


Link to post
Share on other sites

Posted

You can try using this.

#include "WinHttp.au3"

Global $hWINHTTP_STATUS_CALLBACK = DllCallbackRegister("__WINHTTP_STATUS_CALLBACK", "none", "handle;dword_ptr;dword;ptr;dword")

Global $Return = _GetPage("http://www.google.com/")
Switch @error
    Case 0
        ConsoleWrite("Html:" & $Return & @CR)
        ; here is where you can check for a string
    Case Else
        ConsoleWrite(@error&@CR)
EndSwitch


Func _GetPage($Page)

    If Not $Page Then Return SetError(1, 0, 0)
    Local $Crack = _WinHttpCrackUrl($Page)
    If @error Then
        Return SetError(2, 0, 0)
    EndIf

    Local $Port = $INTERNET_DEFAULT_PORT

    If Not $Crack[0] Then $Crack[0] = "http"

    If StringLower($Crack[0]) == "http" Then $Port = $INTERNET_DEFAULT_HTTP_PORT
    If StringLower($Crack[0]) == "https" Then $Port = $INTERNET_DEFAULT_HTTPS_PORT

    ConsoleWrite("Target set: " & $Crack[0]&"://"&$Crack[2]&$Crack[6]&$Crack[7]&@CR)

    Local $hWnd = _WinHttpOpen("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
    If @error Then Return SetError(3, 0, 0)

    ;_WinHttpSetTimeouts($hWnd, 0, 5000, 5000, 2500)

    _WinHttpSetStatusCallback($hWnd, $hWINHTTP_STATUS_CALLBACK)

    Local $hConnect = _WinHttpConnect($hWnd, $Crack[2], $Port)
    If @error Then
        _WinHttpCloseHandle($hConnect)
        _WinHttpCloseHandle($hWnd)
        Return SetError(4, 0, 0)
    EndIf

    Local $hRequest = _WinHttpOpenRequest($hConnect, "GET", $Crack[6], Default, $Crack[0]&"://"&$Crack[2]&$Crack[6]&$Crack[7])
    If Not $hRequest Then
        _WinHttpCloseHandle($hConnect)
        _WinHttpCloseHandle($hWnd)
        Return SetError(5, 0, 0)
    EndIf

    _WinHttpSendRequest($hRequest, "", "")
    If @error Then
        _WinHttpCloseHandle($hRequest)
        _WinHttpCloseHandle($hConnect)
        _WinHttpCloseHandle($hWnd)
        Return SetError(6, 0, 0)
    EndIf

    _WinHttpReceiveResponse($hRequest)
    If @error Then
        _WinHttpCloseHandle($hRequest)
        _WinHttpCloseHandle($hConnect)
        _WinHttpCloseHandle($hWnd)
        Return SetError(7, 0, 0)
    EndIf

    Local $Data
    If _WinHttpQueryDataAvailable($hRequest) Then
        Do
            $Data &= _WinHttpReadData($hRequest, 0)
        Until @error
    Else
        _WinHttpCloseHandle($hRequest)
        _WinHttpCloseHandle($hConnect)
        _WinHttpCloseHandle($hWnd)
        Return SetError(8, 0, 0)
    EndIf

    _WinHttpCloseHandle($hWnd)
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    Return SetError(0, 0, $Data)
EndFunc

Func __WINHTTP_STATUS_CALLBACK($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
    #forceref $hInternet, $iContext, $pStatusInformation, $iStatusInformationLength
    ; Interpret the status
    Local $sStatus
    Switch $iInternetStatus
        Case $WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
            $sStatus = "Closing the connection to the server"
        Case $WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
            $sStatus = "Successfully connected to the server"
        Case $WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
            $sStatus = "Connecting to the server."
        Case $WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
            $sStatus = "Successfully closed the connection to the server"
        Case $WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
            $sStatus = "Data is available to be retrieved with WinHttpReadData"
        Case $WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
            $sStatus = "An HINTERNET handle has been created: " & $hInternet
        Case $WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING
            $sStatus = "This handle value has been terminated: " & $hInternet
        Case $WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
            $sStatus = "The response header has been received and is available with WinHttpQueryHeaders"
        Case $WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE
            $sStatus = "Received an intermediate (100 level) status code message from the server"
        Case $WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
            $sStatus = "Successfully found the IP address of the server"
        Case $WINHTTP_CALLBACK_STATUS_READ_COMPLETE
            $sStatus = "Data was successfully read from the server"
        Case $WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE
            $sStatus = "Waiting for the server to respond to a request"
        Case $WINHTTP_CALLBACK_STATUS_REDIRECT
            $sStatus = "An HTTP request is about to automatically redirect the request"
        Case $WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
            $sStatus = "An error occurred while sending an HTTP request"
        Case $WINHTTP_CALLBACK_STATUS_REQUEST_SENT
            $sStatus = "Successfully sent the information request to the server"
        Case $WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
            $sStatus = "Looking up the IP address of a server name"
        Case $WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED
            $sStatus = "Successfully received a response from the server"
        Case $WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
            $sStatus = "One or more errors were encountered while retrieving a Secure Sockets Layer (SSL) certificate from the server"
        Case $WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
            $sStatus = "Sending the information request to the server"
        Case $WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE
            $sStatus = "The request completed successfully"
        Case $WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE
            $sStatus = "Data was successfully written to the server"
    EndSwitch
    ; Print it
    ConsoleWrite(">" & $sStatus & @CRLF)
EndFunc    ;==>__WINHTTP_STATUS_CALLBACK

Share this post


Link to post
Share on other sites

Posted (edited)

$s_Src = BinaryToString((InetRead("somesite"))
If StringInStr($s_Src, "Index of ftp://ibiblio.org") Then
    ;; Good to go
Else
    ;; Houston we have a problem.
EndIf

Edited by GEOSoft

Share this post


Link to post
Share on other sites

Posted (edited)

Hi Geo,

I think this is nearly perfect but for some reason I get some errors when running, am I missing something? Is there some extra code I need to put on top of what you gave me? Maybe an include?

$s_Src = BinaryToString((InetRead("www.ibiblio.org/pub/multimedia/mutopia/BachJS/BWV16/Cantata_16_no_5/"))
If StringInStr($s_Src, "Index of") Then
;; Good to go
MsgBox(0, "Is text there?", "yes text is there")
Else
;; Houston we have a problem.
MsgBox(0, "Is text there?", "no, text isn't there")
EndIf

Here are the errors from console:

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "D:xtra-dfAutoITexamples_brunofind-text-on-page.au3" /UserParams

+>13:06:33 Starting AutoIt3Wrapper v.2.1.0.33 Environment(Language:0409 Keyboard:00000409 OS:WIN_XP/Service Pack 3 CPU:X64 OS:X86)

>Running AU3Check (1.54.22.0) from:C:Program FilesAutoIt3

D:xtra-dfAutoITexamples_brunofind-text-on-page.au3(1,107) : ERROR: syntax error

$s_Src = BinaryToString((InetRead("www.ibiblio.org/pub/multimedia/mutopia/BachJS/BWV16/Cantata_16_no_5/"))

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

D:xtra-dfAutoITexamples_brunofind-text-on-page.au3(2,36) : ERROR: syntax error

If StringInStr($s_Src, "Index of") Then

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

D:xtra-dfAutoITexamples_brunofind-text-on-page.au3(5,1) : ERROR: syntax error

Else

^

D:xtra-dfAutoITexamples_brunofind-text-on-page.au3 - 3 error(s), 0 warning(s)

!>13:06:33 AU3Check ended. Press F4 to jump to next error.rc:2

>Exit code: 0 Time: 0.405

Edited by xuzo

Share this post


Link to post
Share on other sites

Posted

Hi Geo,

Thank you so much for everybody for the help by the way!

....but still not working...! ;(

Here is the code raw since the forum seems to strip things:

$s_Src = BinaryToString(InetRead("www.ibiblio.org/pub/multimedia/mutopia/BachJS/BWV16/Cantata_16_no_5/"))

If StringInStr($s_Src, "Index of") Then

;; Good to go

MsgBox(0, "Is text there?", "yes text is there")

Else

;; Houston we have a problem.

MsgBox(0, "Is text there?", "no, text isn't there")

EndIf

Even if the text "Index of" shows up on the page, my message box still says it's not there!

Share this post


Link to post
Share on other sites

Posted

Ooops. My bfault

For some reason I just noticed that the rest of your URL is missing.

You need everything including the page. Like default.htm or whatever.

Share this post


Link to post
Share on other sites

Posted (edited)

Hi Geo,

Just noticed you are from Vancouver Island! I used to do a lot of jobs from Vancouver to Nanaimo, nice jobs also with the long feery rides! ;) I live in Thailland now but sometimes miss Canada...!

But anyways...the URL has no extension because it's a folder...there HAS to be a way to do this...I hope...!

All I want to do is check if a page is online, if it is keep doing something, if not move to something else....

Cheers!

Edited by xuzo

Share this post


Link to post
Share on other sites

Posted

Have you even tried the code I gave you?

#include "WinHttp.au3"

Global $hWINHTTP_STATUS_CALLBACK = DllCallbackRegister("__WINHTTP_STATUS_CALLBACK", "none", "handle;dword_ptr;dword;ptr;dword")

Global $Return = _GetPage("http://www.ibiblio.org/pub/multimedia/mutopia/BachJS/BWV16/Cantata_16_no_5/")
Switch @error
Case 0
ConsoleWrite("+>Html Returned: " & $Return & @CR)
; here is where you can check for a string
Case Else
ConsoleWrite("!>Error:" & @error & @CR)
EndSwitch


Func _GetPage($Page)

If Not $Page Then Return SetError(1, 0, 0)
Local $Crack = _WinHttpCrackUrl($Page)
If @error Then
Return SetError(2, 0, 0)
EndIf

Local $Port = $INTERNET_DEFAULT_PORT

If Not $Crack[0] Then $Crack[0] = "http"

If StringLower($Crack[0]) == "http" Then $Port = $INTERNET_DEFAULT_HTTP_PORT
If StringLower($Crack[0]) == "https" Then $Port = $INTERNET_DEFAULT_HTTPS_PORT

ConsoleWrite("Target set: " & $Crack[0]&"://"&$Crack[2]&$Crack[6]&$Crack[7]&@CR)

Local $hWnd = _WinHttpOpen("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
If @error Then Return SetError(3, 0, 0)

;_WinHttpSetTimeouts($hWnd, 0, 5000, 5000, 2500)

_WinHttpSetStatusCallback($hWnd, $hWINHTTP_STATUS_CALLBACK)

Local $hConnect = _WinHttpConnect($hWnd, $Crack[2], $Port)
If @error Then
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hWnd)
Return SetError(4, 0, 0)
EndIf

Local $hRequest = _WinHttpOpenRequest($hConnect, "GET", $Crack[6], Default, $Crack[0]&"://"&$Crack[2]&$Crack[6]&$Crack[7])
If Not $hRequest Then
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hWnd)
Return SetError(5, 0, 0)
EndIf

_WinHttpSendRequest($hRequest, "", "")
If @error Then
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hWnd)
Return SetError(6, 0, 0)
EndIf

_WinHttpReceiveResponse($hRequest)
If @error Then
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hWnd)
Return SetError(7, 0, 0)
EndIf

Local $Data
If _WinHttpQueryDataAvailable($hRequest) Then
Do
$Data &= _WinHttpReadData($hRequest, 0)
Until @error
Else
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hWnd)
Return SetError(8, 0, 0)
EndIf

_WinHttpCloseHandle($hWnd)
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
Return SetError(0, 0, $Data)
EndFunc

Func __WINHTTP_STATUS_CALLBACK($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
#forceref $hInternet, $iContext, $pStatusInformation, $iStatusInformationLength
; Interpret the status
Local $sStatus
Switch $iInternetStatus
Case $WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
$sStatus = "Closing the connection to the server"
Case $WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
$sStatus = "Successfully connected to the server"
Case $WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
$sStatus = "Connecting to the server."
Case $WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
$sStatus = "Successfully closed the connection to the server"
Case $WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
$sStatus = "Data is available to be retrieved with WinHttpReadData"
Case $WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
$sStatus = "An HINTERNET handle has been created: " & $hInternet
Case $WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING
$sStatus = "This handle value has been terminated: " & $hInternet
Case $WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
$sStatus = "The response header has been received and is available with WinHttpQueryHeaders"
Case $WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE
$sStatus = "Received an intermediate (100 level) status code message from the server"
Case $WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
$sStatus = "Successfully found the IP address of the server"
Case $WINHTTP_CALLBACK_STATUS_READ_COMPLETE
$sStatus = "Data was successfully read from the server"
Case $WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE
$sStatus = "Waiting for the server to respond to a request"
Case $WINHTTP_CALLBACK_STATUS_REDIRECT
$sStatus = "An HTTP request is about to automatically redirect the request"
Case $WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
$sStatus = "An error occurred while sending an HTTP request"
Case $WINHTTP_CALLBACK_STATUS_REQUEST_SENT
$sStatus = "Successfully sent the information request to the server"
Case $WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
$sStatus = "Looking up the IP address of a server name"
Case $WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED
$sStatus = "Successfully received a response from the server"
Case $WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
$sStatus = "One or more errors were encountered while retrieving a Secure Sockets Layer (SSL) certificate from the server"
Case $WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
$sStatus = "Sending the information request to the server"
Case $WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE
$sStatus = "The request completed successfully"
Case $WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE
$sStatus = "Data was successfully written to the server"
EndSwitch
; Print it
ConsoleWrite(">" & $sStatus & @CRLF)
EndFunc ;==>__WINHTTP_STATUS_CALLBACK

It works, and so does the example geo posted, you just need to add the scheme name.

$s_Src = BinaryToString(InetRead("http://www.ibiblio.org/pub/multimedia/mutopia/BachJS/BWV16/Cantata_16_no_5/"))
If StringInStr($s_Src, "Index of") Then
    ;; Good to go
    MsgBox(0, "Is text there?", "yes text is there")
Else
    ;; Houston we have a problem.
    MsgBox(0, "Is text there?", "no, text isn't there")
EndIf

Share this post


Link to post
Share on other sites

Posted (edited)

Hi

Yes I does work now! Bingo!

I got the revised code from Geo (that you posted on top) working, what does "add the scheme name" mean? Whatever you did to the code it works now! ;)

Thanks,

Edited by xuzo

Share this post


Link to post
Share on other sites

Posted (edited)

Hi guys,

This is soooooooo cool that I got it working that I figured I might as well push my luck a little!

Trying to make it work with a text file now:

$s_Src = BinaryToString ShellExecute("notepad.exe", @ScriptDir & "text.txt", "", "OPEN")
If StringInStr($s_Src, "this is text") Then
; Good to go
MsgBox(0, "Is text there?", "yes text is there")
Else
; Houston we have a problem.
MsgBox(0, "Is text there?", "no, text isn't there")
EndIf

Of course I'm doing something wrong as usual....!

Edited by xuzo

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.