Http Request <Solved>

PhoenixCNCPhoenixCNC Member Posts: 12
edited 2017-08-04 in NAV Three Tier
I have a web service I can call to retrieve a XML document. In Navision 2013 which Automation variable do I have to declare.
Thanks



Best Answer

Answers

  • JuhlJuhl Member Posts: 724
    HttpWebRequest and HttpWebResponse among other.
    And not Automations, but DOTNET.
    Follow me on my blog juhl.blog
  • RockWithNAVRockWithNAV Member Posts: 1,139
    Stop using Automations USe Dotnet variables as Automations no more from 2016 version I believe.
  • PhoenixCNCPhoenixCNC Member Posts: 12
    What is the Subtype for the DotNot Variable?
  • ftorneroftornero Member Posts: 524
    Here is an example:
    // Request	         DotNet	  System.Net.HttpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    // URL Text
    // ResponseStream	  DotNet  	System.IO.Stream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    
    
    Request := Request.HttpWebRequest;
    URL := 'http://boi.org.il/currency.xml';
    Request := Request.Create(URL);
    Request.UseDefaultCredentials := TRUE;
    Request.Method('GET');
    Request.ContentType('text/xml');
    ResponseStream := Request.GetResponse().GetResponseStream();
    
  • mdPartnerNLmdPartnerNL Member Posts: 802
    DOTNET is obsolete too shortly.
  • PhoenixCNCPhoenixCNC Member Posts: 12
    ftornero wrote: »
    Here is an example:
    // Request	         DotNet	  System.Net.HttpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    // URL Text
    // ResponseStream	  DotNet  	System.IO.Stream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    
    
    Request := Request.HttpWebRequest;
    URL := 'http://boi.org.il/currency.xml';
    Request := Request.Create(URL);
    Request.UseDefaultCredentials := TRUE;
    Request.Method('GET');
    Request.ContentType('text/xml');
    ResponseStream := Request.GetResponse().GetResponseStream();
    

    Hi, thank you for this. If i wanted to take this response an edit the document and then save the file, roughly how would I do that?
  • ftorneroftornero Member Posts: 524

    Looks like this URL ('http://boi.org.il/currency.xml') doesn't work, so I changed to other that works, and here is the code:
    
    OnRun()
    WebService;
    
    WebService()
    Request := Request.HttpWebRequest;
    URL := 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
    Request := Request.Create(URL);
    Request.UseDefaultCredentials := TRUE;
    Request.Method('GET');
    Request.ContentType('text/xml');
    
    IF GetResponse(Request, XMLDoc) THEN
      ShowXML(XMLDoc);
    
    GetResponse(Request : DotNet "System.Net.HttpWebRequest";VAR XMLDocOut : DotNet "System.Xml.XmlDocument") : Boolean
    ResponseStream := Request.GetResponse().GetResponseStream();
    IF NOT ISNULL(ResponseStream) THEN BEGIN
      CreateArrayBytes(Buffer, 2048);
      FileName := FileMgt.ServerTempFileName('');
      FileStream := FileStream.FileStream(FileName, FileMode.Create, FileAccess.Write);
      readBytes := ResponseStream.Read(Buffer, 0, Buffer.Length);
      WHILE readBytes > 0 DO BEGIN
        FileStream.Write(Buffer, 0, readBytes);
        readBytes := ResponseStream.Read(Buffer, 0, Buffer.Length);
      END;
      FileStream.Close();
      XMLDocOut := XMLDocOut.XmlDocument;
      XMLDocOut.Load(FileName);
      EXIT(TRUE);
    END;
    
    EXIT(FALSE);
    
    ShowXML(XMLDoc : DotNet "System.Xml.XmlDocument")
    FileName := FileMgt.ServerTempFileName('');
    XMLDoc.Save(FileName);
    FileName2 := FileMgt.ClientTempFileName('XML');
    FileMgt.DownloadToFile(FileName, FileName2);
    HYPERLINK(FileName2);
    
    CreateArrayBytes(VAR Buffer : DotNet "System.Array";LengthBuffer : Integer)
    NetType := NetType.GetType('System.Byte', FALSE);
    Buffer := Buffer.CreateInstance(NetType, LengthBuffer);
    

    And here you can see the variables used.
    BJECT Codeunit 50005 Test HttWebRequest
    {
      OBJECT-PROPERTIES
      {
        Date=28/07/17;
        Time=13:02:07;
        Modified=Yes;
        Version List=WS;
      }
      PROPERTIES
      {
        OnRun=BEGIN
                WebService;
              END;
    
      }
      CODE
      {
    
        PROCEDURE WebService@1000000001();
        VAR
          Request@1000000000 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";
          URL@1000000002 : Text[250];
          XMLDoc@1000000007 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";
        BEGIN
          Request := Request.HttpWebRequest;
          URL := 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
          Request := Request.Create(URL);
          Request.UseDefaultCredentials := TRUE;
          Request.Method('GET');
          Request.ContentType('text/xml');
    
          IF GetResponse(Request, XMLDoc) THEN
            ShowXML(XMLDoc);
        END;
    
        PROCEDURE GetResponse@1000000005(Request@1000000001 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";VAR XMLDocOut@1000000002 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument") : Boolean;
        VAR
          ResponseStream@1000000008 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream";
          FileStream@1000000011 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
          FileMode@1000000010 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileMode";
          FileAccess@1000000009 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileAccess";
          Buffer@1000000004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array";
          FileName@1000000007 : Text;
          FileMgt@1000000005 : Codeunit 419;
          readBytes@1000000012 : Integer;
        BEGIN
          ResponseStream := Request.GetResponse().GetResponseStream();
          IF NOT ISNULL(ResponseStream) THEN BEGIN
            CreateArrayBytes(Buffer, 2048);
            FileName := FileMgt.ServerTempFileName('');
            FileStream := FileStream.FileStream(FileName, FileMode.Create, FileAccess.Write);
            readBytes := ResponseStream.Read(Buffer, 0, Buffer.Length);
            WHILE readBytes > 0 DO BEGIN
              FileStream.Write(Buffer, 0, readBytes);
              readBytes := ResponseStream.Read(Buffer, 0, Buffer.Length);
            END;
            FileStream.Close();
            XMLDocOut := XMLDocOut.XmlDocument;
            XMLDocOut.Load(FileName);
            EXIT(TRUE);
          END;
    
          EXIT(FALSE);
        END;
    
        PROCEDURE ShowXML@1000000010(XMLDoc@1000000000 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument");
        VAR
          FileName@1000000003 : Text;
          FileName2@1000000002 : Text;
          FileMgt@1000000001 : Codeunit 419;
        BEGIN
          FileName := FileMgt.ServerTempFileName('');
          XMLDoc.Save(FileName);
          FileName2 := FileMgt.ClientTempFileName('XML');
          FileMgt.DownloadToFile(FileName, FileName2);
          HYPERLINK(FileName2);
        END;
    
        LOCAL PROCEDURE CreateArrayBytes@1000000016(VAR Buffer@1000000001 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array";LengthBuffer@1000000002 : Integer);
        VAR
          NetType@1000000000 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Type";
        BEGIN
          NetType := NetType.GetType('System.Byte', FALSE);
          Buffer := Buffer.CreateInstance(NetType, LengthBuffer);
        END;
    
        BEGIN
        END.
      }
    }
    
  • PhoenixCNCPhoenixCNC Member Posts: 12
    ftornero wrote: »
    Looks like this URL ('http://boi.org.il/currency.xml') doesn't work, so I changed to other that works, and here is the code:

    Thanks for the reply, I'm getting an error saying the root element is missing, this is the top of the xml document
    <!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
    <!-- saved from url=(0062)http://192.168.128.90/viewPCB.cgi?PCB=01213 BV-00009 TOP -->
    <html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

    is it possible to remove this before saving the file? and how?
  • ftorneroftornero Member Posts: 524
    Well, I thought that the file you got was XML, but looks like is HTML.

    In the procedure GetResponse, you can change the way the file is managed and instead of load a XMLDocument with it , first try to clean up a little.

    If you can post the complete file I can take a look and see how to do it.

    Regads.
  • PhoenixCNCPhoenixCNC Member Posts: 12
    I have code to edit the document to make it xml friendly, however this only works when I directly load the file. How can I edit the Instream / Big text?
  • ftorneroftornero Member Posts: 524
    Well, how I said in the procedure GetResponse you have a file (FileName) you can return this variable and edit with your code.
  • PhoenixCNCPhoenixCNC Member Posts: 12
    Thank you for your help, I have One more question though,
    My document looks like this now simplified. I've done select node as such
    xmlNodeListDataHeader := xmlDoc.SelectNodes('hr/table/thead/tr/tr'); as well as xmlNodeListData := xmlDoc.SelectNodes('hr/table/tbody/tr/td');
    Now what I need to do is get for each 'Location,Component, Comment'
    or 'C4,PN-0000170,1NF 0402 50V C0G 5%' in each node. I understand how I can use something like txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
    but how in the below example could I apply this?
    <?xml version="1.0" encoding="UTF-8"?>
    <hr>
    <table>
    <thead class="DataTable">
    <tr class="DataTable">
    <th class="DataTable">Line</th>
    <th class="DataTable">Location</th>
    <th class="DataTable">Component</th>
    <th class="DataTable">Comment</th>
    <th class="DataTable">Stock Location</th>
    <th class="DataTable">X</th>
    <th class="DataTable">Y</th>
    <th class="DataTable">Z</th>
    <th class="DataTable">Angle</th>
    <th class="DataTable">Group</th>
    <th class="DataTable">Notes</th>
    </tr>
    </thead>
    <tbody>
    <tr class="DataTable">
    <th class="DataTable">1</th>
    <td class="DataTable">C4</td>
    <td class="DataTable">PN-0000170</td>
    <td class="DataTable">1NF 0402 50V C0G 5%</td>
    <td class="DataTable">0</td>
    <td class="DataTableNumeric">81.318mm</td>
    <td class="DataTableNumeric">47.984mm</td>
    <td class="DataTableNumeric">0.000mm</td>
    <td class="DataTableNumeric">-90</td>
    <td class="DataTable">0</td>
    </tr>
    <tr class="DataTable">
    <th class="DataTable">2</th>
    <td class="DataTable">C9</td>
    <td class="DataTable">PN-1007815</td>
    <td class="DataTable">10PF 0402 50V C0G 1%</td>
    <td class="DataTable">0</td>
    <td class="DataTableNumeric">86.116mm</td>
    <td class="DataTableNumeric">47.489mm</td>
    <td class="DataTableNumeric">0.000mm</td>
    <td class="DataTableNumeric">180</td>
    <td class="DataTable">0</td>
    </tr>
    </table>
    </hr>





  • JuhlJuhl Member Posts: 724
    Follow me on my blog juhl.blog
  • PhoenixCNCPhoenixCNC Member Posts: 12
    Thank you ftornero that is fantastic!
  • krikikriki Member, Moderator Posts: 9,115
    [Topic moved from 'NAV/Navision Classic Client' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.