Get long url from any short or redirect url
you don't need a library to do that, you can simply use XMLHttpRequest
.
i created a snack : snack,
but only work with redirect URL not shorted ones like : shorturl.at/egzG4
Hope this helps you.
Get full url from shorten url using python
First method
As suggested, one way to accomplish the task would be to use the official api to bitly, which has, however, limitations (e.g., no more than 15 shortUrl
's per request).
Second method
As an alternative, one could just avoid getting the contents, e.g. by using the HEAD
HTTP method instead of GET
. Here is just a sample code, which makes use of the excellent requests package:
import requests
l=['bit.ly/1bdDlXc','bit.ly/1bdDlXc',.......,'bit.ly/1bdDlXc']
for i in l:
print requests.head("http://"+i).headers['location']
How to get a long url from a short url
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
Google URL Shortener API: Converting a long URL to short
Finally found the solution.
Instead of adding key as a post param, appended it to the URL itself. like
https://www.googleapis.com/urlshortener/v1/url?key={API_KEY}
and it worked as expected.
Simplest way to get a long URL for a shortened URL in Cocoa?
UPDATE: I just saw your comment and realised it's following the redirect.
See the delegate method: connection:willSendRequest:redirectResponse:
, which tells you it's doing a redirect to this new request, based on the previous response.
You can get the expanded URL either from the new request here, or from the Location header of the redirect response.
Discussion If the delegate wishes to
cancel the redirect, it should call
the connection object’s cancel method.
Alternatively, the delegate method can
return nil to cancel the redirect, and
the connection will continue to
process. This has special relevance in
the case where redirectResponse is not
nil. In this case, any data that is
loaded for the connection will be sent
to the delegate, and the delegate will
receive a connectionDidFinishLoading
or connection:didFailLoadingWithError:
message, as appropriate.
Original answer follows...
Use NSURLConnection
with a delegate. In your delegate's connection:didReceiveResponse:
method, fetch allHeaderFields
and read the value of the "Location" header.
Something like:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Expanded URL = %@", [[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Location"]);
}
I'd create a little URLExpander
class to do this personally, with a signature something like:
+(void)asyncExpandURL:(NSURL *)aURL didExpandTarget:(id)target selector:(SEL)selector;
Then just pass back two arguments in your message, one for the short URL, one for the long.
Related Topics
Why Is Safe Navigation Better Than Using Try in Rails
What Is the Use of "#!/Usr/Local/Bin/Ruby -W" at the Start of a Ruby Program
How to Password-Protect My /Sidekiq Route (I.E. Require Authentication for the Sidekiq::Web Tool)
Activemodel::Missingattributeerror Occurs After Deploying and Then Goes Away After a While
Fresh Installs of Rvm and Ruby 2.1.1 - Dyld Library/Pathing Error
How to Turn Off Automatic Stylesheet/JavaScript Generation on Rails 3.1
Recommended Approach to Monkey Patching a Class in Ruby
Converting Utc Timestamp to Iso 8601 in Ruby
Ruby Way to Check for String Palindrome
Ruby/Rails CSV Parsing, Invalid Byte Sequence in Utf-8
How to Get Constants Defined by Ruby's Module Class via Reflection
Rails: Money Gem Converts All Amounts to Zero
How to Update Ruby with Homebrew