httpRequest -> getURL -> NAV/NAS Download From the internet

g00fyg00fy Member Posts: 2
I need to write a function in a CU, that will download data from given url and hopefully give me info about downloading time. This function is used in CU that runs on NAS (shelude). The function should return True if everything went OK, and False if there were any problems.

My Probelm:
1. Everything works fine, unless there is a "networking problem".
2. When there is a "networking problem" (example, unplugged ETH cable), then Automation variables, will throw Exceptions that I can't handle in Navision (or can I?)
3. When using XMLHTTP40 I can't download files when varAsync=True (don't know why), and I can download files when varAsync=FALSE, but when there is a "networking problem" it starts to throw exceptions.

Does Any 1 know what to do here?

I know that http://www.mibuso.com/dlinfo.asp?FileID=952 works, but this works only when varAsync=TRUE & the "checking of the status" is done on OnTimer trigger. Also the embeded function in the form (where varAsync=False) has the same issues as my functions.

Function nr. 1 (AFAIK this works better, but when varAsync=TRUE, I can't get anything)
PROCEDURE GetURL@1000000001(VAR response@1000000007 : BigText;url@1000000009 : Text[1024];VAR timeout@1000000001 : Integer) : Boolean;
    VAR
      httpRequest@1000000002 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{F6D90F16-9C73-11D3-B32E-00C04F990BB4}:'Microsoft XML, v6.0'.XMLHTTP";
      WinHttpRequest@1000000003 : Automation "{662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1:{2087C2F4-2CEF-4953-A8AB-66779B670495}:'Microsoft WinHTTP Services, version 5.1'.WinHttpRequest";
      elapsed@1000000005 : Integer;
      TimeBegin@1000000006 : Time;
      IStream@1000000010 : InStream;
    BEGIN
      
      CREATE(httpRequest);
      httpRequest.open('GET',url,TRUE);
      // True - Does not work ?
      // False - Works only when ETH connection is UP
      TimeBegin:=TIME;
      httpRequest.send();
      COMMIT;
      REPEAT
        BEGIN
          elapsed:=TIME-TimeBegin;

          IF (elapsed>=timeout) THEN
            BEGIN
            CLEAR(httpRequest);
            EXIT(FALSE)
          END;
        END;
      UNTIL ((httpRequest.readyState() = 4));
      IF NOT (httpRequest.status = 200) THEN
        BEGIN
          CLEAR(httpRequest);
          EXIT(FALSE);
        END;
      IStream:= httpRequest.responseStream();
      CLEAR(httpRequest);
      CLEAR(response);
      response.READ(IStream);
      timeout:=timeout-elapsed;
      EXIT(TRUE);
    END;



Function nr. 2 -> This works OK when there are no networking problems, when there are, it throws exceptions.
    PROCEDURE GetURL@1000000001(VAR response@1000000007 : BigText;url@1000000009 : Text[1024];VAR timeout@1000000001 : Integer) : Boolean;
    VAR
      httpRequest@1000000002 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{F6D90F16-9C73-11D3-B32E-00C04F990BB4}:'Microsoft XML, v6.0'.XMLHTTP";
      WinHttpRequest@1000000003 : Automation "{662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1:{2087C2F4-2CEF-4953-A8AB-66779B670495}:'Microsoft WinHTTP Services, version 5.1'.WinHttpRequest";
      elapsed@1000000005 : Integer;
      TimeBegin@1000000006 : Time;
      IStream@1000000010 : InStream;
    BEGIN
      CREATE(WinHttpRequest);
      WinHttpRequest.Open('GET',url,TRUE);
      TimeBegin:=TIME;
      WinHttpRequest.Send;

      IF NOT WinHttpRequest.WaitForResponse(timeout) THEN
        EXIT(FALSE);
      timeout:=TIME-TimeBegin;
      IF NOT (WinHttpRequest.Status =200) THEN
        EXIT(FALSE);
      IStream := WinHttpRequest.ResponseStream;
      response.READ(IStream);
      CLEAR(IStream);
      CLEAR(WinHttpRequest);
      EXIT(TRUE);
    END;


Running NAV2009R2.
Does any 1 have working implementation (even without elapsed time, but with timeout limit) of something similar?

Comments

Sign In or Register to comment.