How to Read Webpage in X++ in Ax


There seems to be no limit to what you can do with Microsoft Dynamics Ax, aka Axapta. For instance, reading a webpage.

In the old days (version 3), this was possible using the WinInet class.

In Ax 2009, just use CLR interop (common language runtime interoperability).

Classes in assemblies that are managed by the common language runtime (CLR) can be accessed in X++ code. This feature of Microsoft Dynamics AX is called .NET interop from X++. 

.NET interop from X++ is useful when you want your X++ code to access the functionalities in a CLR managed assembly. This includes assemblies that are installed with the .NET Framework. It also includes any assemblies that you create with a language such as C# or Visual Basic. 

The .NET interop from X++ feature works in the direction from X++ calling into CLR managed assemblies. For information about .NET interop in the other direction, see the following topics: 
  • Proxy Classes for .NET Interop to X++
  • Integration with X++ Objects from Visual Studio
  • .NET Business Connector Overview
For example:

staticvoidWebpageRead(Args _args)
{
System.Net.WebClientmyWebClient;
System.IO.StreammyStream;
System.IO.StreamReadermyStreamReader;
str content;
strmyURL="https://www.google.co.in/?gws_rd=ssl";
    ;
myWebClient = newSystem.Net.WebClient();
myStream=myWebClient.OpenRead(myURL);
myStreamReader = newSystem.IO.StreamReader(myStream);
content=myStreamReader.ReadToEnd();
info(content);
}

In version 3, you can use the WinInet Class. Like this:

staticvoidWebSiteRead(Args _args)
{
    WinInet inet = newWinInet();
inthdl;
str content;
BinaryIO _io;
    ;
hdl = inet.internetOpenUrl('http://www.google.com');
content = inet.internetReadFile(hdl);
    _io = newBinaryIO('c:\\test.txt', 'W');
    _io.write(content);
inet.internetCloseHandle(hdl);
}

Related Posts

Previous
Next Post »

Thanks for comments.....