HTTP GET

bhalpinbhalpin Member Posts: 309
Hi.

This question really has two parts:

The simple question is how to get a page from the web into a variable. I know it's through the use of an Automation variable, but which one and what method(s)? (I've been beating my head against a lot of walls 'cuz that's the best way to learn ... but it hurts now.)

The bigger question is this: I'm not an MS environment guro, I see all kinds of cool looking Automation classes through NAV, and I'm sorta comfortable with COM concepts in a differernt context/environment, but, what reference(s) would I use as a guide to what the classes do (for me in NAV particularly)?

Thank's in advance

Comments

  • kinekine Member Posts: 12,562
    You can e.g. use "'Microsoft XML, v3.0'.XMLHTTP" if you want to read XML file and the code will be something as:
    IF ISCLEAR(XMLDOMhttp) THEN
      CREATE(XMLDOMhttp);
    XMLDOMhttp.open('GET', MyURL, TRUE);
    YIELD;
    XMLDOMhttp.send();
    IF ISCLEAR(XMLDOMDocument) THEN
      CREATE(XMLDOMDocument);
    XMLDOMDocument := XMLDOMhttp.responseXML();
    ...
    

    Or you can use automation "'Microsoft WinHTTP Services, version 5.1'.WinHttpRequest" with the Open and WaitForResponse functions to get the data (but there will be some limitation what you can do with the result I think).
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • bhalpinbhalpin Member Posts: 309
    Hi Kamil.

    Thanks - I'm almost there - but not getting any usable data. Here's what I've got:
    Name            DataType    Length  Subtype
    XMLDOMhttp      Automation          'Microsoft XML, v3.0'.XMLHTTP
    XMLDOMDocument  Automation          'Microsoft XML, v3.0'.DOMDocument
    XMLNode         Automation          'Microsoft XML, v3.0'.IXMLDOMNode
    URL             Text        128
    XMLData         Text        256
    XMLDOMMgmt      Codeunit            BizTalk XML DOM Management
    
    URL := 'http://www.mibuso.com';
    IF ISCLEAR(XMLDOMhttp) THEN
      CREATE(XMLDOMhttp);
    XMLDOMhttp.open('GET',URL,TRUE);
    YIELD;
    XMLDOMhttp.send();
    IF ISCLEAR(XMLDOMDocument) THEN
      CREATE(XMLDOMDocument);
    XMLDOMDocument := XMLDOMhttp.responseXML();
    
    // This doesn't work, XMLData is alway's empty
    XMLData := XMLDOMDocument.text;
    
    // I tried this, but still XMLData is alway's empty
    IF XMLDOMMgmt.FindNode(XMLDOMDocument,'/html/head/title',XMLNode) THEN
      XMLData := XMLNode.text;
    

    I've got to be missing something really simple. Can you spot it?

    Thanks (big time) in advance.
  • SavatageSavatage Member Posts: 7,142
    Hyperlink('MyURL');

    still makes me happy :)
  • bhalpinbhalpin Member Posts: 309
    Thanks.

    HyperLink opens IE on the URL, but I want the contents of the page in a C/AL variable so I can parse values from it.

    Bob
  • garakgarak Member Posts: 3,263
    edited 2009-01-16
    Have you check if there are datas in the xml? And the node exist?
    Store the xml as file on hdd and take a look into
    Do you make it right, it works too!
  • SavatageSavatage Member Posts: 7,142
    bhalpin wrote:
    Thanks.
    HyperLink opens IE on the URL, but I want the contents of the page in a C/AL variable so I can parse values from it.
    Bob
    Oh :oops: :lol:
  • bhalpinbhalpin Member Posts: 309
    Have you check if there are datas in the xml? And the node exist?

    Well, in the example I posted, I am using a URL I know exists (www.mibuso.com). Also, I would think that the line XMLData := XMLDOMDocument.text; would return the entire page, regardless of what nodes may or may not exist. But XMLData is alway's empty, no matter what URL I try.
  • bhalpinbhalpin Member Posts: 309
    Yikes.

    All I want to do is get the contents of a web page into a variable and I've been at it the whole day now. Here's the latest dead-end:
    Name        DataType    Subtype   Length
    WinHttpReq  Automation  'Microsoft WinHTTP Services, version 5.1'.WinHttpRequest
    VHttpData   Variant
    HttpData    Text                  1024
    Type        Text                  30
    
    URL := 'http://www.mibuso.com';
    IF ISCLEAR(WinHttpReq) THEN
      CREATE(WinHttpReq);
    WinHttpReq.Open('GET',URL,FALSE);
    WinHttpReq.Send;
    IF NOT WinHttpReq.WaitForResponse() THEN
      ERROR('Timed out');
    
    // Alway's displays '200' and 'OK'
    MESSAGE(
      'Status: '+FORMAT(WinHttpReq.Status())+'\'+
      'StatusText: '+WinHttpReq.StatusText()
    );
    // Get the response body into a variant
    VHttpData := WinHttpReq.ResponseBody;
    // Now, what is it?
    Type := 'Unknown';
    IF VHttpData.ISRECORD          THEN Type :='RECORD';
    IF VHttpData.ISFILE            THEN Type :='FILE';
    IF VHttpData.ISACTION          THEN Type :='ACTION';
    IF VHttpData.ISCODEUNIT        THEN Type :='CODEUNIT';
    IF VHttpData.ISAUTOMATION      THEN Type :='AUTOMATION';
    IF VHttpData.ISBOOLEAN         THEN Type :='BOOLEAN';
    IF VHttpData.ISOPTION          THEN Type :='OPTION';
    IF VHttpData.ISINTEGER         THEN Type :='INTEGER';
    IF VHttpData.ISDECIMAL         THEN Type :='DECIMAL';
    IF VHttpData.ISCHAR            THEN Type :='CHAR';
    IF VHttpData.ISTEXT            THEN Type :='TEXT';
    IF VHttpData.ISCODE            THEN Type :='CODE';
    IF VHttpData.ISDATE            THEN Type :='DATE';
    IF VHttpData.ISTIME            THEN Type :='TIME';
    IF VHttpData.ISBINARY          THEN Type :='BINARY';
    IF VHttpData.ISDATEFORMULA     THEN Type :='DATEFORMULA';
    IF VHttpData.ISTRANSACTIONTYPE THEN Type :='TRANSACTIONTYPE';
    IF VHttpData.ISINSTREAM        THEN Type :='INSTREAM';
    IF VHttpData.ISOUTSTREAM       THEN Type :='OUTSTREAM';
    MESSAGE('Type is '+Type);
    

    You guessed it, the type is 'Unknown'.

    SURELY this can't be that hard! ](*,)
  • ara3nara3n Member Posts: 9,256
    please use 'Microsoft HTML Object Library'.HTMLXMLHttpRequest automation
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • bhalpinbhalpin Member Posts: 309
    Ok, here's the attempt:
    Name                DataType      Subtype     Length
    HTMLXMLHttpRequest  Automation    'Microsoft HTML Object Library'.HTMLXMLHttpRequest
    URL                 Text                      128
    
    URL := 'http://www.mibuso.com';
    IF ISCLEAR(HTMLXMLHttpRequest) THEN
      CREATE(HTMLXMLHttpRequest);
    HTMLXMLHttpRequest.open('GET',URL,FALSE);
    HTMLXMLHttpRequest.send;
    

    Here's the result:
    Could not create an instance of the OLE control or automation server identified by GUID={3050F1C5-98B5-11CF-BB82-00AA00BDCE0B} 4.0:{3051040B-98B5-11CF-BB82-00AA00BDCE0B}:'Microsoft HTML Object Library'.HTMLXMLHttpRequest.

    ](*,) #-o
Sign In or Register to comment.