views:

178

answers:

3

I would like to determine what the long url of a short url is. I have tried using http HEAD requests, but very few of the returned header fields actually contain any data pertaining to the destination/long url.

Is there: 1. Any way to determine the long url? 2. If so, can it be done without downloading the body of the destination?

Thank you

+6  A: 

Issue an HTTP GET request, don't follow the redirect, analyse the Location header. That's where the target of redirection is.

Specifically in Cocoa, use an asynchronous request with a delegate, handle the didReceiveResponse in the delegate. The first response will be the redirection one. Once you extract the URL in the handler, call [cancel] on the connection.

EDIT: depending on the provider, HEAD instead of GET might or might not work. And if you don't follow the redirect, the response data won't be loaded anyway, so there's no transmission overhead to having a GET.

Seva Alekseyev
+2  A: 

Do a HEAD and look for the Location header.

% telnet bit.ly 80
Trying 168.143.173.13...
Connected to bit.ly.
Escape character is '^]'.
HEAD /cwz5Jd HTTP/1.1
Host: bit.ly

HTTP/1.1 301 Moved
Server: nginx/0.7.42
Date: Fri, 12 Mar 2010 18:37:46 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Set-Cookie: _bit=4b9a89fa-002bd-030af-baa08fa8;domain=.bit.ly;expires=Wed Sep  8 14:37:46 2010;path=/; HttpOnly
Location: http://www.engadget.com/2010/03/12/motorola-milestone-with-android-2-1-hitting-bulgaria-by-march-20/?utm_source=twitterfeed&utm_medium=twitter
MIME-Version: 1.0
Content-Length: 404
calmh
This ended up to be the most correct answer. The complete answer includes doing a head request as recommended in this answer by calmh. The response containing the location header can be extracted from the "connection willSendRequest...." delegate method, not the didReceiveResponse delegate method.
Run Loop
HEAD does not always work, but GET generally does. The only caveat with GET is that the connection must be cancelled after url extraction to prevent downloading of the body (as suggested by Seva below)
Run Loop
JK: You mean `connection:willSendRequest:redirectResponse:`. I thought your comment was wrong until I looked it up and saw the third component of the selector.
Peter Hosey
+3  A: 

LongUrlPlease offers an API which expands short urls.

Ben Griswold