these two lines say it all:
thus the URL being requested is actually
really I think the problem is something subtle in the way the data is being parsed. Or maybe it's a syntax error somewhere? I don't have the right machinery running to try your code sample out myself, and I'm very rusty on my VBScript syntax.
Debugging is a tough job, and it's a skill that takes practice. Start by "tracing [en.wikipedia.org]" values to see what they are during the execution of the code. Fixing this function should take a few minutes, not several days.
Tracing is probably the most important debugging technique you must learn, and it's much simpler than, say, bug discovery by systematic amputation, or dissecting the call stack.
The key here is to learn some debugging techniques so you can find out what the problem is. I'm guessing that once you figure out the problem, the solution will be immediately obvious.