Section 7.11. Downloading Content from the Web


7.11. Downloading Content from the Web

The System.Net.WebClient class offers a DownloadString method that can be used to pull content directly from a URL. Because these examples are pulled from dynamic content on the Internet, the exact values in the output may change over time:

     MSH D:\MshScripts> $wc = new-object System.Net.WebClient     MSH D:\MshScripts> $content = $wc.DownloadString("http://www.oreilly.com")     MSH D:\MshScripts> $content | measure-object -words -lines -characters     Lines               Words               Characters          Property     -----               -----               ----------          --------     1108                4554                54957

WebClient is not particular about the type of content it downloads. If a URL points to XML content, it is perfectly legitimate to cast the string to an XmlDocument and work with it from there:

     MSH D:\MshScripts> $xml = [Xml]$wc.DownloadString     ("http://www.oreillynet.com/rss/render/160.rss")     MSH D:\MshScripts> $xml.GetElementsByTagName("item") | format-table title     title     -----     Exchange Server Cookbook     ASP.NET 2.0: A Developer's Notebook     Knoppix Pocket Reference     Firefox Secrets: A Need-to-Know Guide     Mapping Hacks     Mac OS X Tiger Pocket Guide     Learning Unix for Mac OS X Tiger     Assembling Panoramic Photos: A Designer's Notebook     DHTML Utopia: Modern Web Design Using JavaScript and DOM     Toad Pocket Reference for Oracle, 2nd Edition     Mac OS X Tiger for Unix Geeks




Monad Jumpstart
Monad Jumpstart
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 117

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net