Net::HTTP
I decided to move away from the delightful open-uri method of dealing with feed retrieval in favor of using the Net::HTTP module because of the lack of control over http redirection that open-uri has.
Unfortunately, this has proven to be a lot more difficult than I would have expected—even with the source code for open-uri right in front of me, I’m still having trouble figuring out why, for example, Slashdot’s rss feed seems to be giving me a 301 Moved Permanently redirect to http://slashdot.org/index.rss, and then a 503 Service Unavailable error when I try to retrieve that.
This is irksome because the feed shows up fine in Firefox, I can retrieve it easily with open-uri (which wraps Net::HTTP, so I know it’s not an issue with Net::HTTP itself), and the same code I’m using above manages to retrieve everything but Slashdot quite well.
Basically, I know I’ve got a phantom bug in here somewhere… just gotta track it down somehow…
<grumble>There should be cheat codes for programming.</grumble>
Update (2 hours later):
Wrong:1 2 3 4 5 |
Net::HTTP.start('slashdot.org', 80) do |http| response = http.request_get( 'http://slashdot.org/index.rss', http_headers) end |
(Apparently translates to this.)
Right:1 2 3 4 |
Net::HTTP.start('slashdot.org', 80) do |http| response = http.request_get('/index.rss', http_headers) end |
I’m pretty much kicking myself, because that really should have been obvious…
Also… I should fix Typo so that it’ll let me put <pre> blocks into the comments. Posting code snippets in the comments doesn’t really work too well without it, as I just discovered (tried to put the post update into a comment)…
Update: Code in the comments works now.
Leave a Response