Author Topic: wget  (Read 3120 times)

JRS

  • Guest
wget
« on: November 06, 2010, 04:26:54 pm »
I was poking around the PowerBASIC forum and noticed a post about how to use PowerBASIC to download a web page into a string. If I had to write that much code to do a simple task
like get a web page, I would do it in C. Ongoing discussion trying to get the below code to work.

Code: Text
  1. ' SED_PBCC
  2.  
  3.  
  4.  
  5. '----------------------------------------------------------------------------(')
  6.  
  7.  
  8.  
  9. #INCLUDE "win32api.inc"
  10.  
  11.  
  12.  
  13. '----------------------------------------------------------------------------(')
  14.  
  15.  
  16.  
  17. %INTERNET_OPEN_TYPE_PRECONFIG = 0               ' use registry configuration
  18. %INTERNET_FLAG_PRAGMA_NOCACHE = &H00000100              ' asking wininet to add "pragma: no-cache"
  19. %INTERNET_FLAG_NEED_FILE = &H00000010           ' need a file for this request
  20. %INTERNET_FLAG_NO_CACHE_WRITE = &H04000000              ' don't write this item to the cache
  21. %INTERNET_FLAG_FROM_CACHE = &H01000000          ' use offline semantics
  22. %INTERNET_FLAG_RELOAD = &H80000000              ' retrieve the original item
  23.  
  24. %http_blocksize = 4096
  25.  
  26.  
  27.  
  28. '----------------------------------------------------------------------------(')
  29.  
  30.  
  31.  
  32. DECLARE FUNCTION INTERNETOPEN LIB "wininet.dll" ALIAS "InternetOpenA" _
  33.   ( _
  34.   lpszAgent AS ASCIIZ, _                ' in
  35.   BYVAL dwAccessType AS DWORD, _                ' in
  36.   lpszProxy AS ASCIIZ, _                ' in
  37.   lpszProxyBypass AS ASCIIZ, _          ' in
  38.   BYVAL dwFlags AS DWORD _              ' in
  39.   ) _
  40.   AS DWORD
  41. DECLARE FUNCTION INTERNETOPENURL LIB "wininet.dll" ALIAS "InternetOpenUrlA" _
  42.   ( _
  43.   BYVAL hInternet AS DWORD, _           ' in
  44.   lpszUrl AS ASCIIZ, _          ' in
  45.   lpszHeaders AS ASCIIZ, _              ' in
  46.   BYVAL dwHeadersLength AS DWORD, _             ' in
  47.   BYVAL dwFlags AS DWORD, _             ' in
  48.   BYVAL dwContext AS DWORD _            ' in
  49.   ) _
  50.   AS DWORD
  51. DECLARE FUNCTION INTERNETREADFILE LIB "wininet.dll" ALIAS "InternetReadFile" _
  52.   ( _
  53.   BYVAL hFile AS DWORD, _               ' in
  54.   BYVAL lpBuffer AS BYTE PTR, _         ' in
  55.   BYVAL dwNumberOfBytesToRead AS DWORD, _               ' in
  56.   lpdwNumberOfBytesRead AS DWORD _              ' out
  57.   ) _
  58.   AS LONG
  59. DECLARE FUNCTION INTERNETCLOSEHANDLE LIB "wininet.dll" _
  60.   ALIAS "InternetCloseHandle" _
  61.   ( _
  62.   BYVAL hInternet AS DWORD _            ' in
  63.   ) _
  64.   AS LONG
  65.  
  66.  
  67.  
  68. '----------------------------------------------------------------------------(')
  69.  
  70.  
  71.  
  72. FUNCTION PBMAIN AS LONG
  73.         '
  74.         LOCAL TS1 AS STRING
  75.         LOCAL URL AS STRING
  76.         '
  77.         URL = "http://www.powerbasic.com"
  78.         STDOUT "Content-type: text/html"
  79.         STDOUT
  80.         STDOUT "<table bgcolor=""white""><tr><td><hr> Page Downloaded Via Cgi Script : " + URL + "<hr><br /><br /></tr></td></table>"
  81.         '
  82.         CALL urldownloadtostring( URL, TS1 )
  83.         '
  84.         STDOUT TS1;
  85.         '
  86. END FUNCTION
  87.  
  88.  
  89.  
  90. '----------------------------------------------------------------------------(')
  91.  
  92.  
  93.  
  94. FUNCTION urldownloadtostring( szurl AS STRING, szbuf AS STRING ) AS LONG
  95.         '
  96.         DIM bytesread AS LOCAL LONG
  97.         DIM er AS LOCAL LONG
  98.         DIM hfile AS LOCAL LONG
  99.         DIM hinternetsession AS LOCAL DWORD
  100.         DIM lbuf AS LOCAL LONG
  101.         DIM sztmp AS LOCAL STRING
  102.         DIM sztmpbuf AS LOCAL STRING
  103.         '
  104.         DO
  105.                 '
  106.                 hinternetsession = INTERNETOPEN( "pb/dll 7.04", %INTERNET_OPEN_TYPE_PRECONFIG, BYVAL 0, BYVAL 0, 0 )
  107.                 '
  108.                 IF hinternetsession = 0 THEN
  109.                         er = - 1
  110.                         EXIT DO
  111.                 END IF
  112.                 '
  113.                 sztmp = "pragma: no-cache"
  114.                 '
  115.                 hfile = INTERNETOPENURL( hinternetsession, BYVAL STRPTR( szurl ), _
  116.                   BYVAL STRPTR( sztmp ), BYVAL LEN( sztmp ), %INTERNET_FLAG_PRAGMA_NOCACHE OR _
  117.                   %INTERNET_FLAG_NO_CACHE_WRITE OR %INTERNET_FLAG_RELOAD, 0 )
  118.                 '
  119.                 IF hfile = 0 THEN
  120.                         er = - 1
  121.                         EXIT DO
  122.                 END IF
  123.                 '
  124.                 szbuf = SPACE$( %http_blocksize )
  125.                 '
  126.                 DO
  127.                         '
  128.                         IF INTERNETREADFILE( hfile, BYVAL STRPTR( szbuf ), %http_blocksize, BYREF lbuf ) = 0 THEN
  129.                                 er = - 1
  130.                                 EXIT DO
  131.                         END IF
  132.       '
  133.                         IF lbuf = 0 THEN
  134.                                 EXIT DO
  135.                         END IF
  136.       '
  137.                         IF LEN( sztmpbuf ) <( lbuf + bytesread ) THEN
  138.         '
  139.                                 IF LEN( sztmpbuf ) = 0 THEN
  140.                                         sztmpbuf = SPACE$( &h100000 )
  141.                                 ELSE
  142.                                         sztmpbuf = sztmpbuf + sztmpbuf
  143.                                 END IF
  144.         '
  145.                         END IF
  146.       '
  147.                         MID$( sztmpbuf, bytesread + 1, lbuf ) = LEFT$( szbuf, lbuf )
  148.       '
  149.                         bytesread = bytesread + lbuf
  150.       '
  151.                 LOOP
  152.     '
  153.                 EXIT DO
  154.     '
  155.         LOOP
  156.   '
  157.         IF hfile THEN
  158.      INTERNETCLOSEHANDLE hfile
  159.   end if
  160.   '
  161.         IF hinternetsession THEN
  162.      INTERNETCLOSEHANDLE hinternetsession
  163.   end if
  164.   '
  165.         IF er THEN
  166.      FUNCTION = er
  167.      bytesread = 0
  168.   end if
  169.   '
  170.         szbuf = LEFT$( sztmpbuf, bytesread )
  171.   '
  172. END FUNCTION
  173.  

Code: Text
  1. OPEN "allbasic.info:80" for SOCKET AS #1
  2. PRINT #1,"GET http://localhost/ HTTP/1.0\n\n"
  3. webpage = ""
  4. WHILE NOT EOF(1)
  5.   LINE INPUT #1, content
  6.   webpage &= content
  7. WEND
  8. PRINT webpage
  9. CLOSE(1)
  10.  
« Last Edit: November 07, 2010, 07:07:56 pm by JRS »