Unless you are doing this for educational reasons then why reinvent the wheel? As well as the wonderful perl modules mentioned by people above, there is also a most excellent standalone tool:
WGET
Thanks, dingus, I'll check it out. Part of the motivation is educational, part of it is business related, but mostly I was unaware there was such a utility out there.
Very cool.