An example in powerbasic ' Demonstrate multiple requests/responses within a single HTTP session ' (Requests are made sequentially) ' Upload a file in XFER_CHUNK_SZ chunks #COMPILE EXE "Upload.exe" #INCLUDE "WIN32API.INC" #INCLUDE "WinHTTP.inc" #INCLUDE "WinHTTPWrap.inc" %XFER_CHUNK_SZ = 64000 ' 64k TYPE CHUNK_HEAD_TYPE ChunkNum AS DWORD ' TotChunks AS DWORD ' DataLen AS DWORD ' Payload Bytes TotData AS DWORD ' sFileName AS STRING * 32 ' Shorten long filenames END TYPE '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ FUNCTION FileUpload( sPath AS STRING, sFileName AS STRING ) AS LONG LOCAL hSession, hConnect, hRequest AS DWORD LOCAL i, TotChunks, RetVal, hFile, LenHead, BytesRemain AS LONG LOCAL sAgent, sHost, sVerb, sData, sHeader, sBody, sResrc, sPOST AS STRING LOCAL pData AS BYTE PTR LOCAL Head AS CHUNK_HEAD_TYPE ' Chunk variables Structure sAgent = "File Upload Example" ' sHost = "www.YourServer.com" sResrc = "/cgi-bin/FCGIUpload.exe" ' FastCGI application sVerb = "POST" Head.sFileName = sFileName LenHead = SIZEOF(CHUNK_HEAD_TYPE) ' length of prepended head '=========== '- Read the File hFile = FREEFILE ' OPEN sPath+sFileName FOR BINARY AS hFile ' Make a byte array with the contents of your file IF ERR THEN FUNCTION = -1 : EXIT FUNCTION Head.TotData = LOF(hFile) ' TotFileBytes IF Head.TotData = 0 THEN CLOSE hFile : FUNCTION = -2 : EXIT FUNCTION GET$ hFile, Head.TotData, sData ' read the whole file into a string CLOSE hFile ' pData = STRPTR(sData) FUNCTION = Head.TotData ' Default '=========== DO hSession = WinHTTPInitLib(sAgent, 0) ' IF hSession < 1 THEN FUNCTION = -7 : EXIT FUNCTION ' '=============== hConnect = WinHTTPConnectSrvr( hSession, sHost ) ' WinHttpConnect() IF hConnect < 1 THEN FUNCTION = -15 : EXIT LOOP ' '=============== Head.TotChunks = Head.TotData\%XFER_CHUNK_SZ ' IF Head.TotChunks * %XFER_CHUNK_SZ < Head.TotData THEN INCR Head.TotChunks ' One more for the remainder bytes IF Head.TotChunks = 0 THEN INCR Head.TotChunks ' BytesRemain = Head.TotData ' '=============== FOR i = 1 TO Head.TotChunks ' hRequest = WinHTTPInitRequest( hConnect, sVerb, sResrc ) ' Create Request handle. IF hRequest < 1 THEN FUNCTION = -17 : EXIT FOR ' Head.DataLen = MIN(%XFER_CHUNK_SZ, BytesRemain) Head.ChunkNum = i '- Send POST Request RetVal = WinHttpSendRequestData( hRequest, 0, SIZEOF(Head)+Head.DataLen ) ' Prep Request IF RetVal < 0 THEN FUNCTION = -21 : EXIT FOR ' RetVal = WinHTTPSendData( hRequest, VARPTR(Head), SIZEOF(Head)) ' Send the Header IF RetVal < 0 THEN FUNCTION = -22 : EXIT FOR ' RetVal = WinHTTPSendData( hRequest, pData, Head.DataLen) ' Send the file IF RetVal < 0 THEN FUNCTION = -23 : EXIT FOR ' pData = pData + Head.DataLen ' advance pointer to start of next chunk BytesRemain = BytesRemain - Head.DataLen ' decrement the bytes remaining '- Receive the Response RetVal = WinHTTPGetResponse(hRequest) ' IF RetVal < 0 THEN FUNCTION = -33 : EXIT FOR ' '- Read the Response Header HTTP Code RetVal = WinHTTPResponseHeaderInt( hRequest, %WINHTTP_QUERY_STATUS_CODE ) IF RetVal = 0 THEN FUNCTION = -34 : EXIT FOR ELSEIF RetVal > 299 THEN FUNCTION = -35 : EXIT FOR END IF '- Check the Response Body for an Error message RetVal = WinHTTPResponseBody( hRequest, sBody ) ' IF RetVal < 0 THEN FUNCTION = -37 : EXIT FOR ' IF INSTR(sBody, "ERROR:") THEN FUNCTION = -38 : EXIT FOR '- Close Request Handle IF hRequest THEN RetVal = WinHttpCloseHandle(hRequest) IF RetVal < 0 THEN FUNCTION = -39 : EXIT FOR ' NEXT i '=============== EXIT LOOP LOOP ' IF hConnect THEN RetVal = WinHttpCloseHandle(hConnect) IF RetVal < 0 THEN FUNCTION = -40 : EXIT FUNCTION' IF hSession THEN RetVal = WinHttpCloseHandle(hSession) IF RetVal < 0 THEN FUNCTION = -41 : EXIT FUNCTION' END FUNCTION '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ FUNCTION PBMAIN LOCAL RetVal AS LONG RetVal = FileUpload( "C:\Examples\HTTP\", "WinHTTP.gif" ) IF RetVal < 0 THEN MSGBOX "RetVal="+STR$(RetVal) + $CRLF + WinHTTPLastError,64,"Error" ELSE MSGBOX "All Done" END IF END FUNCTION '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ |