Validating a website
Validating a website
How do I validate the existence of a website. I am looking for pages that ONLY return an HTTP response of 200, and the following works;
URL$ = "http://www.purebasic.com/"
Header$ = GetHTTPHeader(URL$)
If StringField(Header$, 2, " ") = "200"
Debug "Valid Link"
Else
Debug "Broken Link"
EndIf
HOWEVER, if I give URL$ a site that does NOT exist, the program hangs. How can I test if a URL exists before proceeding?
Thanks
URL$ = "http://www.purebasic.com/"
Header$ = GetHTTPHeader(URL$)
If StringField(Header$, 2, " ") = "200"
Debug "Valid Link"
Else
Debug "Broken Link"
EndIf
HOWEVER, if I give URL$ a site that does NOT exist, the program hangs. How can I test if a URL exists before proceeding?
Thanks
Re: Validating a website
What about a simple Ping-Test?
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
Re: Validating a website
Well, I am new to Purebasic and I was hoping that there would be a function or so that would even allow me to ping port 80, but I can't even find that. Since you mentioned, it, how would I do that?
Thanks
Thanks
Re: Validating a website
Maybe the Forum-Search will give you a helpful result.BillyBob wrote:Well, I am new to Purebasic and I was hoping that there would be a function or so that would even allow me to ping port 80, but I can't even find that. Since you mentioned, it, how would I do that?
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Validating a website
If you put some procs of Freak's together with one of mine you have a library that will do as you ask. It will come back and tell you within one second if the url exists or not:
You can save everything above the 'END TEST CODE' line as a .pbi and use IncludeFile, this way you don't have to watch the sausages being made 
Code: Select all
; By fr34k
#DNS_TYPE_A = $0001 ; // 1
#DNS_TYPE_NS = $0002 ; // 2
#DNS_TYPE_MD = $0003 ; // 3
#DNS_TYPE_MF = $0004 ; // 4
#DNS_TYPE_CNAME = $0005 ; // 5
#DNS_TYPE_SOA = $0006 ; // 6
#DNS_TYPE_MB = $0007 ; // 7
#DNS_TYPE_MG = $0008 ; // 8
#DNS_TYPE_MR = $0009 ; // 9
#DNS_TYPE_NULL = $000a ; // 10
#DNS_TYPE_WKS = $000b ; // 11
#DNS_TYPE_PTR = $000c ; // 12
#DNS_TYPE_HINFO = $000d ; // 13
#DNS_TYPE_MINFO = $000e ; // 14
#DNS_TYPE_MX = $000f ; // 15
#DNS_TYPE_TEXT = $0010 ; // 16
#DNS_QUERY_STANDARD = $00000000
#DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = $00000001
#DNS_QUERY_USE_TCP_ONLY = $00000002
#DNS_QUERY_NO_RECURSION = $00000004
#DNS_QUERY_BYPASS_CACHE = $00000008
#DNS_QUERY_NO_WIRE_QUERY = $00000010
#DNS_QUERY_NO_LOCAL_NAME = $00000020
#DNS_QUERY_NO_HOSTS_FILE = $00000040
#DNS_QUERY_NO_NETBT = $00000080
#DNS_QUERY_WIRE_ONLY = $00000100
#DNS_QUERY_RETURN_MESSAGE = $00000200
#DNS_QUERY_TREAT_AS_FQDN = $00001000
#DNS_QUERY_DONT_RESET_TTL_VALUES = $00100000
#DNS_QUERY_RESERVED = $ff000000
Enumeration ; DNS_FREE_TYPE
#DnsFreeFlat = 0
#DnsFreeRecordList
#DnsFreeParsedMessageFields
EndEnumeration
Structure DNS_A_DATA
IpAddress.l
EndStructure
Structure DNS_PTR_DATAA
*pNameHost
EndStructure
Structure DNS_RECORD
*pNext.DNS_RECORD;
pName.s
wType.w
wDataLength.w
StructureUnion
DW.l
S.l
EndStructureUnion
dwTtl.l
dwReserved.l
; Note: The Union def is incomplete. see DNS_RECORD in the psdk for more fields
StructureUnion
A.DNS_A_DATA
PTR.DNS_PTR_DATAA
CNAME.DNS_PTR_DATAA
EndStructureUnion
EndStructure
Prototype DnsQuery_A(name.p-ascii, wType.w, fOptions.l, *aopServers, *ppQueryResultSet, *pReserved)
Prototype DnsRecordListFree(*RecordList, FreeType)
Global DnsQuery_A.DnsQuery_A, DnsRecordListFree.DnsRecordListFree
; =========================================================
; Load the Dnsapi.dll. Use it just like OpenLibrary()
; Library is the #Library number for the new lib (can be #PB_Any)
;
Procedure LoadDnsApi(Library)
Protected Result
Result = OpenLibrary(Library, "Dnsapi.dll")
If Result
If Library = #PB_Any
Library = Result
EndIf
DnsQuery_A = GetFunction(Library, "DnsQuery_A")
DnsRecordListFree = GetFunction(Library, "DnsRecordListFree")
If DnsQuery_A = 0 Or DnsRecordListFree = 0
CloseLibrary(Library)
Result = 0
EndIf
EndIf
ProcedureReturn Result
EndProcedure
; Get the IP for the server name
; returns 0 on failure
;
Procedure.l DnsQuery(ServerName$)
Protected IP = 0, CName$, *Record.DNS_RECORD
If DnsQuery_A And DnsRecordListFree
If DnsQuery_A(ServerName$, #DNS_TYPE_A, #DNS_QUERY_STANDARD, #Null, @*Record.DNS_RECORD, #Null) = 0 And *Record
If *Record\wType = #DNS_TYPE_A ; dns record
IP = *Record\A\IpAddress
ElseIf *Record\wType = #DNS_TYPE_CNAME ; redirection
CName$ = PeekS(*Record\CNAME\pNameHost)
If CName$
IP = DnsQuery(CName$)
EndIf
EndIf
DnsRecordListFree(*Record, #DnsFreeRecordList)
EndIf
EndIf
ProcedureReturn IP
EndProcedure
; Get the name for the given IP
; returns "" on failure
;
Procedure.s ReverseDnsQuery(IP.l)
Protected Name$ = "", *Record.DNS_RECORD
Protected Query$ = Str((IP>>24) & $FF)+"."+Str((IP>>16) & $FF)+"."+Str((IP>>8) & $FF)+"."+Str(IP & $FF)+".IN-ADDR.ARPA" ; ip must be reversed!
If DnsQuery_A And DnsRecordListFree
If DnsQuery_A(Query$, #DNS_TYPE_PTR, #DNS_QUERY_STANDARD, #Null, @*Record.DNS_RECORD, #Null) = 0 And *Record
If *Record\wType = #DNS_TYPE_PTR
Name$ = PeekS(*Record\PTR\pNameHost)
EndIf
DnsRecordListFree(*Record, #DnsFreeRecordList)
EndIf
EndIf
ProcedureReturn Name$
EndProcedure
Procedure TestInetConnection(servername$) ;netmaestro 2009
SendData$ = "Test"
ReplyBuffer$ = Space(SizeOf(ICMP_ECHO_REPLY) + Len(SendData$) + SizeOf(character))
hIcmpFile = IcmpCreateFile_()
dwRetVal = IcmpSendEcho_(hIcmpFile, inet_addr_(servername$), @SendData$, Len(SendData$), #Null, @ReplyBuffer$, Len(ReplyBuffer$) + SizeOf(ICMP_ECHO_REPLY), 1000)
If dwRetVal
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure
;======================================================
; END LIBRARY CODE
;======================================================
If LoadDnsApi(0)
result = DnsQuery("purebasic.com")
If result
Debug TestInetConnection(IPString(result))
Else
Debug 0
EndIf
Else
Debug 0
EndIf

BERESHEIT
Re: Validating a website
Wow, netmaestro, that a lot of code.
I was going to point out that WebGadget() will tell you if the site exists... but he wasn't asking to make a gadget.
I think you have the corner on complex answers.
[oh, does that run on a Mac?]
I was going to point out that WebGadget() will tell you if the site exists... but he wasn't asking to make a gadget.
I think you have the corner on complex answers.

MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Validating a website
Ok, a bit shorter then: (but way less versatile)
Code: Select all
result = RunProgram("Ping", "purebasic.com","", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
Output$ = ""
If result
While ProgramRunning(result)
If AvailableProgramOutput(result)
Output$ + ReadProgramString(result)
EndIf
Wend
CloseProgram(result)
EndIf
loc1 = FindString(output$, "Average = ",1)+10
loc2 = FindString(output$, "ms", loc1)
avg$ = Mid(output$,loc1, loc2-loc1)
avg.i = Val(avg$)
Debug "ping to purebasic.com took "+avg$+" milliseconds"
BERESHEIT
Re: Validating a website
PureLust wrote:Maybe the Forum-Search will give you a helpful result.BillyBob wrote:Well, I am new to Purebasic and I was hoping that there would be a function or so that would even allow me to ping port 80, but I can't even find that. Since you mentioned, it, how would I do that?
I was searching this form for about an hour and a half before I posted the question. Thanks for the input.
Re: Validating a website
Thank you all for posting solutions, I really appreciate it...
- Kwai chang caine
- Always Here
- Posts: 5494
- Joined: Sun Nov 05, 2006 11:42 pm
- Location: Lyon - France
Re: Validating a website
This code don't works with PROXY on my jobnetmaestro wrote:Ok, a bit shorter then: (but way less versatile)Code: Select all
result = RunProgram("Ping", "purebasic.com","", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide) Output$ = "" If result While ProgramRunning(result) If AvailableProgramOutput(result) Output$ + ReadProgramString(result) EndIf Wend CloseProgram(result) EndIf loc1 = FindString(output$, "Average = ",1)+10 loc2 = FindString(output$, "ms", loc1) avg$ = Mid(output$,loc1, loc2-loc1) avg.i = Val(avg$) Debug "ping to purebasic.com took "+avg$+" milliseconds"

Have you a code with the same behaviour with a PROXY ???

Not a destination
Re: Validating a website
Another less complex example code was posted by STARGÅTE and modified by jamba:
http://www.purebasic.fr/english/viewtop ... 98&start=3
http://www.purebasic.fr/english/viewtop ... 98&start=3