Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Working WEBGET/WGET...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Working WEBGET/WGET...

    Code:
    '
    'NOTE:  Why a repost?  Because I wasted thirty minutes until I noticed that
    'the TIMEOUT setting of the TCP connection was overriding the 60 second default
    'with 30 milliseconds, which was obviously far too short for even most fiber-
    'optic network scenarios.  ALSO I added ERR checking.
    '
    '==============================================================================
    '
    ' WEBGET v1.3 - Grab a file from a web site
    ' Copyright (c) 1999-2001 PowerBASIC, Inc. All Rights Reserved.
    '
    '==============================================================================
     
    %port = 80
     
    FUNCTION PBMAIN() AS LONG
     
       LOCAL buffer AS STRING
       LOCAL site AS STRING
       LOCAL file AS STRING
       LOCAL save AS STRING
       LOCAL temp AS STRING
     
       STDOUT "Webget v1.2 - Grab a file from a web site"
       STDOUT "Copyright (c) 1999-2001 PowerBASIC, Inc. All Rights Reserved."
       STDOUT ""
     
       IF LEN(COMMAND$) = 0 OR COMMAND$ = "/?" THEN
          STDOUT "Usage: webget url/file"
          STDOUT ""
          STDOUT " Ex: webget <A HREF="http://www.powerbasic.com/index.html"" TARGET=_blank>http://www.powerbasic.com/index.html"</A> 
          EXIT FUNCTION
       END IF
     
       site = EXTRACT$(LTRIM$(LCASE$(COMMAND$),"http://"), "/")
     
       IF LEN(site) = 0 THEN
          STDOUT "Error! You must specify a web site."
          EXIT FUNCTION
       END IF
     
       file = TRIM$(MID$(COMMAND$, INSTR(LCASE$(COMMAND$), site) + LEN(site)))
     
       IF LEN(file) = 0 THEN
          STDOUT "Error! You must specify a file"
          EXIT FUNCTION
       END IF
     
       save = MID$(file, INSTR(-1, file, "/") + 1)
     
       IF LEN(save) = 0 THEN
         save = EXTRACT$(LTRIM$(site, "www."), ".") + ".html"
       END IF
     
       STDOUT "Connecting to " & site & " ...";
     
       TCP OPEN PORT %port AT site AS #1 TIMEOUT 300000
    
       IF ERR OR ERRAPI THEN
          STDOUT ""
          STDOUT "Could not connect to " & site
          STDOUT "ERR:" & TRIM$(STR$(ERR)) & " ERRAPI:" & TRIM$(STR$(ERRAPI))
          EXIT FUNCTION
       ELSE
          STDOUT " OK!"
          STDOUT ""
       END IF
     
       STDOUT "Retrieving " & site & file & " to " & save & " ";
     
       TCP PRINT #1, "GET " & file & " HTTP/1.1"
       TCP PRINT #1, "Accept: */*"
       TCP PRINT #1, "Accept-Language: en-us"
       TCP PRINT #1, "Host: " & site
       TCP PRINT #1, "Pragma: no-cache"
       TCP PRINT #1, "Referer: <A HREF="http://www.google.com"" TARGET=_blank>http://www.google.com"</A>  
       TCP PRINT #1, "UserAgent: webget 1.3 (www.powerbasic.com)"
       TCP PRINT #1, ""
     
       DO
          TCP RECV #1, 4096, buffer
          temp = temp & buffer
          STDOUT ".";
          '
          'Don't check EOF() in here because brief pauses in file download
          'will leave an empty buffer and set EOF() until you next check
          'and the buffer is full - at which point EOF() will be unset...
          '
          'From TCP RECV PB Help: If count& bytes are not available, Buffer$
          'will receive whatever bytes are available and EOF(fNum&) will
          'return TRUE (non-zero).
          '
          'IF EOF(1) THEN STDOUT "": EXIT DO
          '
          IF LEN(buffer) = 0 THEN STDOUT "": EXIT DO
          IF ERR THEN STDOUT "": EXIT DO
       LOOP
     
       IF ERR THEN STDOUT "ERR Error... (" & TRIM$(STR$(ERR)) & ")"
       IF EOF(1) THEN STDOUT "End of file returned for TCP connection."
       IF LEN(buffer) = 0 THEN STDOUT "Empty download buffer."
     
       buffer = REMAIN$(temp, $CRLF & $CRLF)
       temp = EXTRACT$(temp, $CRLF & $CRLF)
     
       ' ** Display the header information
       STDOUT temp
     
       ' ** Write the data section to a file
       OPEN save FOR BINARY AS #2
       SETEOF #2
       PUT$ #2, buffer
       CLOSE #2
     
       TCP CLOSE #1
     
    END FUNCTION


    [This message has been edited by Chad D. Wood (edited June 18, 2004).]

  • #2
    It only worked after I uncommented

    If Eof(1) Then StdOut "": Exit Do

    Without that, it just sat there.

    Useful though, thanks!

    Dave

    ------------------

    Comment


    • #3
      Code:
      'Only works here with Windows 98 SE by changing:
      'TCP PRINT #1, "GET " & file & " HTTP/1.1" 
      'to
      'TCP PRINT #1, "GET " & file & " HTTP/1.0"


      ------------------
      The world is full of apathy, but who cares?

      Comment


      • #4
        Chad, I tried to compile on XP Home and Win2K, and both times got error 516:

        Error 516 in C:\PBCC30\Samples\WebGet.bas(32:70): DefType, Type id (?%&!#$), or AS... required: HTTP
        Line 32: STDOUT " Ex: webget <A HREF="http://www.powerbasic.com/index.html"" TARGET=_blank>http://www.powerbasic.com/index.html"</A>

        I haven't had time to look into it yet, just an FYI. Thanks for sharing.


        ------------------
        Mark Pruitt
        [email protected]
        sigpicMark Pruitt
        [email protected]

        http://ezreregister.com
        Manage 3rd party BlackBerry software registration codes.

        It's not the only way to do it...just the EZ way!

        Comment


        • #5
          Doh! Just realized I left the URL start/end blocks in when I copied the
          code. Took them out and it works fine!

          ------------------
          Mark Pruitt
          [email protected]
          sigpicMark Pruitt
          [email protected]

          http://ezreregister.com
          Manage 3rd party BlackBerry software registration codes.

          It's not the only way to do it...just the EZ way!

          Comment


          • #6
            Hey Chad/Everyone,

            When I try this code, it sits and waits until the TCP timeout is hit and then it returns the web page. If I uncomment the EOF check within the loop, then it comes back much faster but I seem to be only getting the headers returned. I'm testing against a WebLogic web server and an IIS Web Server. When I don't do the EOF check within the loop, requesting a page from an IIS web server, it just hangs and doesn't seem to ever come back (CORRECTION: It finally came back when I was done writing this message). Any ideas?

            Does the PowerBASIC team have any suggestions?

            Scott

            ------------------
            Scott Wolfington
            http://www.boogietools.com



            [This message has been edited by Scott Wolfington (edited July 07, 2004).]
            Scott Wolfington
            [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

            Comment


            • #7
              re-posted in the "programming the internet" forum. http://www.powerbasic.com/support/pb...ad.php?t=29523

              scott




              [this message has been edited by scott wolfington (edited july 08, 2004).]
              Scott Wolfington
              [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

              Comment


              • #8
                Chad,

                You'll want to update the following line

                Code:
                TCP PRINT #1, "UserAgent: webget 1.3 (www.powerbasic.com)"
                to be this

                Code:
                TCP PRINT #1, "User-Agent: webget 1.3 (www.powerbasic.com)"
                User-Agent is the correct header name.

                Cheers,
                Scott


                ------------------
                Scott Wolfington
                http://www.boogietools.com
                Scott Wolfington
                [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

                Comment


                • #9
                  Hi All

                  I get similar errors but concerning STDOUT. If I comment STDOUT out line by line the error moves down
                  line by line. The first error is hit on the STDOUT "Webget v1.2 - Grab a file from a web site" line
                  Any ideas as to what is wrong, I copied and pasted the prog in from the original message.

                  Cheers Pete


                  Chad, I tried to compile on XP Home and Win2K, and both times got error 516:

                  Error 516 in C:\PBCC30\Samples\WebGet.bas(32:70): DefType, Type id (?%&!#$), or AS... required: HTTP
                  Line 32: STDOUT " Ex: webget <A HREF="http://www.powerbasic.com/index.html"" TARGET=_blank>http://www.powerbasic.com/index.html"</A>

                  I haven't had time to look into it yet, just an FYI. Thanks for sharing.


                  [/QUOTE]



                  ------------------

                  Comment

                  Working...
                  X