Wednesday, 18 January 2017
Main Menu
Web Links
Blog Links
Musical Links
Nerdery Links
Other Neato Links
Reading an XML document from behind a proxy...
User Rating: / 3
Written by WATYF on Thursday, 31 August 2006 (11997 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. Laughing

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. Wink 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. Cool

So without further ado, I present... how to read an XML document, when you're behind a proxy (in 5 lines or less Tongue out)...

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. Tongue out

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
            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

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. Cool



< Prev   Next >


You must javascript enabled to use this form

I don't understand a thing on here! I feel really dumb right now! Thanks......thanks a lot!!!

Posted by Keith, on 09/01/2006 at 21:56

nice! This was exactly what I was looking for, and works a charm. You saved my butt!

Posted by thompsch, on 10/20/2006 at 16:21

Nice one, exactly what I needed. Guess this time around u posted something useful rather than smacking Mac users

Posted by Al Dadze, on 08/07/2009 at 04:52

Perfect, thanks!

Posted by Maf, on 03/08/2011 at 09:15

All those C# nerds out there are too good for a three-lines-of-code solution
- I thought I was the only one who felt like this. A kindred spirit at last!

Thanks for an elegant and simple solution. Worked perfectly. (Except

is deprecated now but just used

Posted by Chris, on 05/16/2013 at 02:39

Page 1 of 1 ( 5 Comments )
©2007 MosCom


© 2017 Musical Nerdery
Joomla! is Free Software released under the GNU/GPL License.