Sample HTTPS Post Using WIN API
Posted: Tue Nov 14, 2017 9:14 pm
Hi
I have a sample of how to post XML using SSL and the Windows API. It was based on some code I found in the PureArea.net but I thought it might be useful for others wanting to use HTTP post in PB.
Simon
I have a sample of how to post XML using SSL and the Windows API. It was based on some code I found in the PureArea.net but I thought it might be useful for others wanting to use HTTP post in PB.
Code: Select all
#XMLDeclaration = "<?xml version="+#DQUOTE$+"1.0"+#DQUOTE$+" encoding="+#DQUOTE$+"UTF-8"+#DQUOTE$+"?>"
#INTERNET_OPEN_TYPE_DIRECT = 1
#HTTP_ADDREQ_FLAG_ADD = $20000000
#HTTP_ADDREQ_FLAG_REPLACE = $80000000
#INTERNET_FLAG_SECURE = $800000 ; https
;#INTERNET_FLAG_SECURE = $0 ; http
#INTERNET_SERVICE_HTTP = 3 ; http
#INTERNET_DEFAULT_HTTPS_PORT = 443 ; https
;#INTERNET_DEFAULT_HTTPS_PORT = 80 ; http
Procedure SendRequest(*toParam.Param) ; I used a pointer to a structure but you can handle this as you like.
Define lnOHnd.i,lnCHnd.i,lnRHnd.i,lcHeader.s,lnSHnd.i,lcRxBuf.s,lnRead.i,lnRetVal.i,lcHost.s,lcURL.s
*toParam\Response = ""
lnOHnd = InternetOpen_("dCPayment",#INTERNET_OPEN_TYPE_DIRECT,"","",0) ; dCPayment is my user agent you can specify your own
lcHost = StrExtract(*toParam\Host1,"","/") ; i.e. no http or https just test.mysite.com
lcURL = "/"+StrExtract(*toParam\Host1,"/","") ; everything after the .com i.e. /dev/showage.dcam?
lnCHnd = InternetConnect_(lnOHnd,lcHost,#INTERNET_DEFAULT_HTTPS_PORT,"","",#INTERNET_SERVICE_HTTP,0,0)
lnRHnd = HttpOpenRequest_(lnCHnd,"POST",lcURL,"","",0,#INTERNET_FLAG_SECURE,0)
lcHeader.s = "Content-Type: text/xml" +Chr(13)+Chr(10) ; used to post XML and you can added more headers if needed separated by Chr(13)+Chr(10)
HttpAddRequestHeaders_(lnRnd,lcHeader,Len(lcHeader), #HTTP_ADDREQ_FLAG_REPLACE | #HTTP_ADDREQ_FLAG_ADD) ; adds the request headers
*toParam\Request=#XMLDeclaration+*toParam\XMLRoot1+*toParam\Request+*toParam\XMLRoot2 ; the XML string however you define it.
lnSHnd = HttpSendRequest_(lnRHnd,"",0,*toParam\Request,Len(*toParam\Request))
lcRxBuf = Space(1024)
; Read until we can't read anymore. The string *toParam\Response will hold what ever we received.
Repeat
lnRetVal = InternetReadFile_(lnRHnd,@lcRxBuf,1024,@lnRead)
*toParam\Response + Left(lcRxBuf,lnRead)
lcRxBuf = Space(1024)
Until (lnRetVal = 1 And lnRead=0 ) Or lnRetVal = 0
If *toParam\Log ; This is my logging function but you can handle this anyway you like.
WriteToLog("REQUEST: https://"+*toParam\Host1+ReplaceString(*toParam\Request,*toParam\Unmasked,*toParam\Masked)+Chr(13)+"RESPONSE: "+*toParam\Response)
EndIf
EndProcedure