Written by WATYF on Thursday, 31 August 2006 (2360 hits)
Category: .NET Programming
Well... it's that time again... I've gone back to messing around with TaskRunner. I'm updating (and adding to) the list of things that I plan to cram into its feature-set, and doing some preliminary code tests. That's not to say that a new version will be out "soon"... but at least I'm finally back to doing some coding. And as a result, I can take a break from the monotony of bashing Mac users, and actually post something useful.  One day into working on TaskRunner again, and I already have run into the typical scenario: I think of something simple that I want to do, and go out in search of a straightforward solution, only to find that no one seems to want to do it the easy way. All those C# nerds out there are too good for a three-lines-of-code solution. They've gotta go making up a bunch of classes and methods and properties and crap just to do what can be done in a few short lines. Well I'm a VB.NET hack, thank you very much... I don't wanna have to add classes and functions... just tell me how to do it in two lines or less.  So without further ado, I present... how to read an XML document, when you're behind a proxy (in 5 lines or less )...
Well... maybe there will be a little more "ado" before I get to it... First, some back-story. You see... if you try to use a WebRequest... or an XMLReader... (or what have you) it usually works just fine... but if you're behind a proxy... then it gives you this wonderful error: The remote server returned an error: (407) Proxy Authentication Required. Now... with a WebRequest, it's easy to get around that... just get the default proxy credentials used for Internet Explorer.... like so: Dim wrq As HttpWebRequest
wrq = WebRequest.Create("http://www.musicalnerdery.com") wrq.Proxy = WebProxy.GetDefaultProxy wrq.Proxy.Credentials = CredentialCache.DefaultCredentials Then... you can return the WebRequest's response however you want. Obviously, you have to have some credentials entered in Internet Explorer for this to work, otherwise, you can specify credentials directly in the code. But either way, it's fairly easy to handle proxies when you're just doing an HTTP request. But.... the XMLReader (and XMLTextReader) objects don't have a "Proxy" property. So how do you pass the default credentials to the XMLReader? Well... if you do a quick Google search, you'd think that you'd have to do all kinds of crap with inheriting classes and what not... but you'd be wrong.  Instead of all that mumbo jumbo... you can just use the exact code above, and pass the resultant stream from the WebRequest straight to the XMLTextReader... like so: Dim rd As XmlTextReader Dim wrq As HttpWebRequest wrq = WebRequest.Create("http://www.musicalnerdery.com/component/option,com_rss/Itemid,0/feed,RSS2.0/no_html,1/") wrq.Proxy = WebProxy.GetDefaultProxy wrq.Proxy.Credentials = CredentialCache.DefaultCredentials rd = New XmlTextReader(wrq.GetResponse.GetResponseStream) You now have an XMLTextReader loaded with the XML document you just retrieved from the URL... and just for kicks... here's a quick way to dump that XML file into a DataSet so you can easily retrieve data from it... Dim ds as New DataSet ds.ReadXml(rd) For i = 0 To ds.Tables("Item").Rows.Count - 1 'Get some random values from the Dataset and do whatever you want with 'em sURL = ds.Tables("Item").Rows(i).Item("link").ToString sTitle = ds.Tables("Item").Rows(i).Item("title").ToString sDesc = ds.Tables("Item").Rows(i).Item("description").ToString Next
So there you have it... pull down an XML file from behind a proxy in 4 lines... and dump it into a DataSet with a couple more... it doesn't get much easier.  WATYF |