C/Side & Automation server Microsoft WinHTTP Services

bhalpinbhalpin Member Posts: 309
Hi.

I'm experimenting with the Automation class 'Microsoft WinHTTP Services, version 5.1'.WinHttpRequest'. It looks to me like I can use this to reach out and grab web content.

So far though, I haven't come up with much. The following code is my 'hacking-to-date':
Vars:
   WinHTTPRequest 
     Type: Automation 
     Automation server: Microsoft WinHTTP Services, version 5.1
     Class: WinHttpRequest
   RText
     Type: BigText

CREATE(WinHTTPRequest);
WinHTTPRequest.Open('GET','http://www.mibuso.com',FALSE);
WinHTTPRequest.Send;
IF WinHTTPRequest.
WaitForResponse(5000) THEN BEGIN
  CLEAR(RText);
  RText.ADDTEXT(WinHTTPRequest.ResponseBody());
  MESSAGE(FORMAT(RText));
END ELSE
  MESSAGE('No Response');

When I run this, I get three odd characters in the MESSAGE window.

If I change the code to use a plain Text variable to store the respone in, I get the error "This data type is not supported by C/Side."

I'm having fun - but not getting very far.

Has anyone used this automation server (and can maybe point out where I'm being a total idiot), or know of any working examples anywhere?

Thanks in advance.

Bob

Comments

  • rvduurenrvduuren Member Posts: 92
    Hello,

    What is it you are trying to achieve with WinHTTP, download content? Check -> Download content with NAV.
    Met vriendelijke groet, best regards,

    Rvduuren
  • bhalpinbhalpin Member Posts: 309
    Hi.

    Thank's for that link - that look exactly like what I'm after.

    The background is I want to download currency echange rate information from a given URL. I started a thread about this earlier and reciived an excellent link to the download section for an XML-based solution.

    I'll probably go with this idea though - it's simpler and required fewer objects.

    Bob
  • garakgarak Member Posts: 3,263
    To download an XML file from a website you can do this for example with this:
    Name	DataType	Subtype	Length
    URL	Text		250
    XMLHTTP	Automation	'Microsoft XML, v4.0'.XMLHTTP40	
    XMLDoc	Automation	'Microsoft XML, v4.0'.DOMDocument40	
    
    ----------------------------
    OnPush()
    Name	DataType	Subtype	Length
    XMLFile	File		
    XMLPort	XMLport	YourDesignedXMLPort
    Stream	InStream		
    
    
    DownloadXMLFile(Filename);
    IF XMLFile.OPEN(Filename) THEN BEGIN
      XMLFile.CREATEINSTREAM(Stream);
      XMLPort.SETSOURCE(Stream);
      IF NOT XMLPort.IMPORT THEN
        error('Could not import the file :-( ');
    end else error('No file to open');
    -----------------------------
    
    DownloadXMLFile(VAR Filename : Text[30])
    IF ISCLEAR(XMLHTTP) THEN
      IF NOT CREATE(XMLHTTP) THEN
        ERROR(Text000);
    
    URL:= HEREYOURURLTOTHEXML;
    
    XMLHTTP.open('GET',URL,FALSE);
    XMLHTTP.send();
    
    IF (XMLHTTP.status = 200) THEN BEGIN
      IF ISCLEAR(XMLDoc) THEN
        IF NOT CREATE(XMLDoc) THEN
          ERROR(Text000);
    
      XMLDoc.async:=FALSE;
      XMLDoc.load(XMLHTTP.responseBody);
    
      IF RemoveNamespace(XMLDoc,XMLDoc) THEN BEGIN
        Filename´:= ENVIRON('TEMP') + '\' + 'Filename.xml';
    
        XMLDoc.save(Filename);
      END;
    
      CLEAR(XMLDoc);
    END ELSE
        ERROR('Can't find URL');
    
    CLEAR(XMLHTTP);
    
    -----------------------
    
    RemoveNamespace(XMLSourceDocument : Automation "'Microsoft XML, v4.0'.DOMDocument40";VAR XMLDestinationDocument : Automation "'Microsof
    Name	DataType	Subtype	Length
    TempTable	Record	Item	
    OutStreamStylesheet	OutStream		
    InStreamStylesheet	InStream		
    XMLStyleSheet	Automation	'Microsoft XML, v4.0'.DOMDocument40	
    
    //here we remove not needed Namespaces
    
    TempTable.Picture.CREATEOUTSTREAM(OutStreamStylesheet);
    TempTable.Picture.CREATEINSTREAM(InStreamStylesheet);
    
    WITH OutStreamStylesheet DO BEGIN
      WRITETEXT('<?xml version="1.0" encoding="UTF-8"?>');
      WRITETEXT('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">');
      WRITETEXT('<xsl:output method="xml" encoding="UTF-8" />');
      WRITETEXT('<xsl:template match="/">');
      WRITETEXT('<xsl:copy>');
      WRITETEXT('<xsl:apply-templates />');
      WRITETEXT('</xsl:copy>');
      WRITETEXT('</xsl:template>');
      WRITETEXT('<xsl:template match="*">');
      WRITETEXT('<xsl:element name="{local-name()}">');
      WRITETEXT('<xsl:apply-templates select="@* | node()" />');
      WRITETEXT('</xsl:element>');
      WRITETEXT('</xsl:template>');
      WRITETEXT('<xsl:template match="@*">');
      WRITETEXT('<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>');
      WRITETEXT('</xsl:template>');
      WRITETEXT('<xsl:template match="text() | processing-instruction() | comment()">');
      WRITETEXT('<xsl:copy />');
      WRITETEXT('</xsl:template>');
      WRITETEXT('</xsl:stylesheet>');
    END;
    
    IF ISCLEAR(XMLStyleSheet) THEN
      IF NOT CREATE(XMLStyleSheet) THEN
        EXIT(FALSE);
    
    IF NOT XMLStyleSheet.load(InStreamStylesheet) THEN
      EXIT(FALSE);
    
    IF ISCLEAR(XMLDestinationDocument) THEN
      IF NOT CREATE(XMLDestinationDocument) THEN
        EXIT(FALSE);
    
    XMLSourceDocument.transformNodeToObject(XMLStyleSheet,XMLDestinationDocument);
    
    EXIT(TRUE);
    ---------------------
    

    So with this little example you can download a XML file from a specific url and import this file with a XMLPort (also you can use the XMLDom)

    Regards
    Do you make it right, it works too!
  • bhalpinbhalpin Member Posts: 309
    Yikes! Never ask a question here unless you're ready to accept *LOTS* of help!

    This is all great stuff people - thanks!

    I was just reading the article at the link below the previous post here came in. This looks really good:

    http://blogs.msdn.com/nav_developer/archive/2008/03/31/sample-of-how-to-read-a-simple-xml-document-using-msxml-dom-in-nav.aspx

    Bob
Sign In or Register to comment.