Get actual URL from TinyURL
I want the user to get the actual URL from the TinyURL or Tiny.cc services, or any other URL redirectors. So is it possible for me to get the actual long URLs from the short redirected URLs, without making a browswer application that runs in the background?
Than开发者_开发百科ks in advance.
Header:
#import "UntitledViewController.h"
@implementation UntitledViewController
- (id)init
{
self = [super init];
if (self)
{
NSURL *url = [NSURL URLWithString:@"http://tinyurl.com/a3cx"];
[self loadTinyURL:url];
}
return self;
}
- (void)loadTinyURL:(NSURL *)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (!connection)
NSLog(@"could not connect with: %@", url);
}
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int statusCode = [httpResponse statusCode];
// http statuscodes between 300 & 400 is a redirect ...
if (response && statusCode >= 300 && statusCode < 400)
NSLog(@"redirecting to : %@", [request URL]);
return request;
}
@end
Implementation:
//
// UntitledViewController.h
// Untitled
//
// Created by tushar chutani on 11-04-19.
// Copyright 2011 Fleetwood park secondary . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UntitledViewController : UIViewController {
}
- (void)loadTinyURL:(NSURL *)url;
@end
The NSURLConnection has a call back connection:willSendRequest:redirectResponse:. At this point you can inspect the redirectResponse to see where you are going.
UPDATE:
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse
{
//Make sure tinyurl is doing the redirection
if([[[redirectResponse URL] host] compare:@"tinyurl.com"
options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
NSLog(@"Redirect Location: %@", [request URL]);
}
//call [connection cancel]; to cancel the redirect and stop receiving data
//return nil; to cancel redirect but continue receiving data
//return request; will continue the redirection as normal
return request;
}
Check out the following code:
#import "TinyURLHandler.h"
@interface TinyURLHandler (Private)
- (void)loadTinyURL:(NSURL *)url;
@end
@implementation TinyURLHandler
- (id)init
{
self = [super init];
if (self)
{
NSURL *url = [NSURL URLWithString:@"http://tinyurl.com/a3cx"];
[self loadTinyURL:url];
}
return self;
}
- (void)loadTinyURL:(NSURL *)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (!connection)
NSLog(@"could not connect with: %@", url);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int statusCode = [httpResponse statusCode];
NSLog(@"%d : %@", statusCode, [NSHTTPURLResponse
localizedStringForStatusCode:statusCode]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSLog(@"finished");
}
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int statusCode = [httpResponse statusCode];
// http statuscodes between 300 & 400 is a redirect ...
if (response && statusCode >= 300 && statusCode < 400)
NSLog(@"redirecting to : %@", [request URL]);
return request;
}
@end
@ openingsposter: I'm not 100% sure why you want to see the original project, I guess it's because you want to figure out what would be a good way to extract the final URL? Well, what I would suggest is you create a delegate and inform the caller once you got the final URL. If you need an example, I can add more source code ...
精彩评论