Announcement

Collapse
No announcement yet.

Programtically Submit Form And Read Results

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

  • Programtically Submit Form And Read Results

    Been digging around the forums but cant find anything.

    I'm using UrlDownLoadToFile to read web pages. Is it possible to simulate a form submit and read the result in the same way? I cant use the following method as the form contents are too large.

    ie. http://myserver/cgi-bin/app.cgi?name...address=street ........ etc

    The form is as follows.

    <form name="input" action="app.cgi" method="get">
    <input type="hidden" name="name" value="steve">
    <input type="hidden" name="address" value="street">

    ...... etc
    </form>
    Last edited by Steve Bouffe; 6 Oct 2008, 01:18 AM.

  • #2
    This may work:

    Code:
    #INCLUDE "WININET.INC"
    %HTTP_BUFFER_LENGTH=16000
    
    FUNCTION WEBREQUEST(sURL AS STRING, FormData AS STRING) EXPORT AS STRING
    
        LOCAL iReturn       AS LONG
        LOCAL iRead         AS LONG
        LOCAL hInternet     AS DWORD
        LOCAL hConn         AS DWORD
        LOCAL hRequest      AS DWORD
        LOCAL sHeaders      AS ASCIIZ*1200
        LOCAL sData         AS STRING
        LOCAL sBuffer       AS STRING
        LOCAL ErrMsg        AS STRING
        LOCAL sReturnData   AS STRING
        LOCAL myurl         AS ASCIIZ*%MAX_PATH
        LOCAL fd            AS STRING
        LOCAL sMethod       AS ASCIIZ*%MAX_PATH
        LOCAL ws            AS ASCIIZ*%MAX_PATH
    
        fd=FormData
    
        ws=sURL
        ws = REMAIN$(ws,"://")
        ws = MID$(ws,INSTR(ws,"/")+1)
    
        hInternet = InternetOpen(BYVAL %NULL, %INTERNET_OPEN_TYPE_PRECONFIG, BYVAL %NULL, BYVAL %NULL, 0)
        IF hInternet = 0 THEN
            ErrMsg = "InternetOpen failed"
            GOTO Finish
        END IF
    
        myurl=EXTRACT$(REMAIN$(sURL, "://"),"/")
    
        hConn = InternetConnect(hInternet, myurl, 80, "", "", %INTERNET_SERVICE_HTTP, 0, 0)
        IF hConn = 0 THEN
            ErrMsg = "InternetConnect failed"
            GOTO Finish
        END IF
    
        sMethod="POST"
        hRequest = httpOpenRequest(hConn, sMethod, ws, "HTTP/1.1", BYVAL %NULL, BYVAL %NULL, %INTERNET_FLAG_RELOAD OR %INTERNET_FLAG_NO_CACHE_WRITE OR %INTERNET_FLAG_PRAGMA_NOCACHE, %NULL)
        IF hRequest = 0 THEN
            ErrMsg = "httpOpenRequest failed."
            GOTO Finish
        END IF
    
        sHeaders = "Content-Type: application/x-www-form-urlencoded"+$CRLF+_
                   "Content-Length: "+FORMAT$(LEN(fd))+$CRLF+_
                   "Connection: Keep-Alive "+$CRLF+$CRLF
    
        sData=fd
    
        IF httpSendRequest(hRequest, sHeaders, LEN(sHeaders), BYVAL STRPTR(sData), LEN(sData)) = 0 THEN
            ErrMsg = "httpSendRequest failed."
            GOTO Finish
        END IF
    
        iRead = 0
        sReturnData = ""
        sBuffer = SPACE$(%HTTP_BUFFER_LENGTH)
        iReturn = InternetReadFile(hRequest, BYVAL STRPTR(sBuffer), %HTTP_BUFFER_LENGTH, iRead)
    
        DO
            
            IF iRead = 0 THEN
                IF iReturn <> 0 THEN
                    EXIT DO
                END IF
            END IF
    
            sReturnData = sReturnData + LEFT$(sBuffer, iRead)
            iRead = 0
            sBuffer = SPACE$(%HTTP_BUFFER_LENGTH)
            iReturn = InternetReadFile(hRequest, BYVAL STRPTR(sBuffer), %HTTP_BUFFER_LENGTH, iRead)
    
        LOOP
    
    Finish:
    
       IF hRequest<>0 THEN
           InternetCloseHandle hRequest
       END IF
       
       IF hConn<>0 THEN
           InternetCloseHandle hConn
       END IF
       
       IF hInternet<>0 THEN
           InternetCloseHandle hInternet
       END IF
       
       FUNCTION = IIF$(TRIM$(ErrMsg)="",sReturnData,ErrMsg)
    
    END FUNCTION
    Adam Drake
    PowerBASIC

    Comment


    • #3
      This works really well, just call with the url and the formdata as "user=john&pass=secret&session=10234" etc.

      I think that this will be of good use to lots of us!
      Last edited by Steve Bouffe; 10 Oct 2008, 03:32 AM.

      Comment


      • #4
        Some time ago I was trying to get URLDownloadToFile to work in a pbcc cgi app with no success.

        I can get the function here WEBREQUEST to work with no problems.

        Although some URLs I can download using WEBREQUEST with no form data, some servers will not work properly.

        Is there any way to modify the code to download just URL's that I can add to my cgi app?

        At the moment I've got a pbcc app running on the server's desktop that monitors a directory. The cgi app dumps a request file and the program running on the server reads the file, performs a URLDownLoadToFile and writes the result in the results directory which the cgi app wait for. Bit of a hack but it deos work.

        Comment

        Working...
        X