Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
FUNCTION GetHttp
LPARAMETERS lcUrl
LOCAL MyResult
LOCAL loHttp as WinHttp.WinHttpRequest.5.1, MyResult
loHTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
loHTTP.Open("GET", lcUrl,.F.)
TRY
loHttp.Send()
lcResult = loHttp.ResponseText
CATCH
lcResult = "* INVALID URL *"
ENDTRY
loHttp = NULL
RETURN lcResult
Clear
? '---------------https://github.com----------------------------------------------------------------'
x = getURLstatus("https://github.com")
? x
? '---------------www.microsoft.com-----------------------------------------------------------------'
x = getURLstatus("www.microsoft.com")
? x
? "---------------http://if protocol prefix included it's not valid with this function--------------"
x = getURLstatus("http://if protocol prefix included it's not valid with this function")
? x
? '---------------https://something.wild------------------------------------------------------------'
x = getURLstatus("https://something.wild")
? x
? '---------------https://google.com/notexistingurlonexistinghost-----------------------------------'
x = getURLstatus("https://google.com/notexistingurlonexistinghost")
? x
Function getURLstatus
Lparameters tcUrl
Local lcRestult, lnStatus
Local loHttp As WinHttp.WinHttpRequest.5.1
loHttp = Createobject("WinHttp.WinHttpRequest.5.1")
Try
loHttp.Open("HEAD", tcUrl,.F.)
loHttp.Send()
lcResult = Transform(loHttp.Status)+" "+loHttp.StatusText
*optional, if status is 200 OK, also return all response headers
If loHttp.Status = 200
lcResult = lcResult+Chr(13)+Chr(10)
lcResult = lcResult+Left(loHttp.GetAllResponseHeaders(),300)+"..."+Chr(13)+Chr(10)
Endif
Catch To loException
lcResult = loException.Message
Endtry
Return lcResult
Good idea. By reading just the HEAD, you have the potential to speed it up if the URL returns a lot of data.That's a good take, if you expand it as follows it'll give you further info from either the Statustext of a request not being status 200 OK or an excpetion message from Winhttp.WinHttpRequest. Notice I changed to HEAD, so the result is only about the status. If you're only nterested in some headers, you can also be selective and only get the wanted headers like loHttp.getResponseHeader("Content-Length") only. Some headers are in all responses, some not, so GetAllResponseHeaders works generally and gives you all info about the response besides the response body you will only get with a GET request.
Whatever suits you. You'd miss out subtleties like a 301 redirect, for example. Or a 503 Service Unavailable that's usually temporarily, even 502 Bad Gateway can be a temporary problem only, so not getting 200 OK and any other status or excecption you don't consider these things.simply return .T. for a 200, and .F. for any exception.
That's probably all that's needed. If Ravi's goal was just knowing a URL is formatted correctly, the function should return true even if the domain doesn't even exist.Well, and if it's all only about that you can just use IsValidURL in that case.
Exactly, and you should know that implication. There's no point in complaining that requesting a valid URL still does not work in one or another aspect, for example.the function should return true even if the domain doesn't even exist.
Validating a URL by making a request to it still also is in the scope of validating it. It's clearly slower but gives you concrete result of the status right now. And depending on the use case. If this textbox Ravi talks about is just the input for the URL to visit, then just do it, if it fails you have the error handling mechanism and can inform the user what's wrong. You can choose to autoexpand or not, etc. And for that use case the "Swiss Army knife" is the better tool.asking an engineer to make a flathead screwdriver and they build you a Swiss Army knife