How to get full url from short? Xcode

I need help on how to get the full url from a short one. For example: http://tinysong.com/HJ9h
If you click this link, the grooveshark URL will open. How can I get the grooveshark source url?
Thanks you

+3
source share
4 answers

Work with short URLs using HTTP redirects. You can configure NSURLConnectionwith a request with a short url and set a delegate. In deletet, execute this method:

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response;

response nil, . . , NSHTTPURLResponse -isKindOfClass:, .

, request, NSURLConnection. URL URL-, .

, , , , , -cancel .


Update. :

#import <Foundation/Foundation.h>

static BOOL stop;

@interface MyDelegate : NSObject
@end

@implementation MyDelegate

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
    NSLog(@"request %@", request);
    NSLog(@"response %@", response);
    return response ? nil : request;
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%s: error %@", __func__, error);
    stop = TRUE;
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"%s", __func__);
    stop = TRUE;
}

@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://tinysong.com/HJ9h"]];
    MyDelegate* del = [[MyDelegate alloc] init];
    [NSURLConnection connectionWithRequest:req delegate:del];
    while (!stop)
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

    [pool drain];
    return 0;
}

:

request <NSURLRequest http://tinysong.com/HJ9h>
response (null)
request <NSURLRequest http://grooveshark.com/#!/s/~/3Xl6OQ?src=11>
response <NSHTTPURLResponse: 0x103500340>
-[MyDelegate connectionDidFinishLoading:]
+3

API e. . LongURL Untiny.

0

curl --head shorturl

, URL.

$ curl --head http://tinysong.com/HJ9h
HTTP/1.1 301 Moved Permanenty
Server: sharkattack!
Date: Sat, 05 May 2012 22:13:10 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Set-Cookie: TinysongSessionID=4518ef13c93199344a23bb9fe9af3e98; expires=Sat, 02-May-2015 22:13:10 GMT; path=/
Location: http://grooveshark.com/#!/s/~/3Xl6OQ?src=11
Cache-Control: max-age=300
Expires: Sat, 05 May 2012 22:18:10 GMT
Vary: Accept-Encoding
X-Hostname: rhl082
X-N-Hostname: RHL082

, URL-, , Location.

, NSTask , .

0

HTTP 301,302 303 , HTTP- URL-...

301, 302 303, , ... , - , 200...

vola, URL-.

Now in terms of getting headers ... I use ASIHTTPRequest for all my network needs ... http://allseeing-i.com/ASIHTTPRequest/How-to-use it has an option to keep track of redirects, so I guess that if you make a request, you can read ResponseHeaders and get a long URL.

Hope that makes sense

0
source

All Articles