2010-12-19 18 views

回答

4

更新:我剛剛看到您的評論,並意識到它遵循重定向。

請參閱委託方法:connection:willSendRequest:redirectResponse:,它告訴您根據以前的響應重定向到這個新請求。

您可以從此處的新請求或重定向響應的位置標題中獲取擴展的URL。

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.

原來的答覆如下......

使用NSURLConnection與委託。在您的代理人的connection:didReceiveResponse:方法中,獲取allHeaderFields並讀取「位置」標題的值。

喜歡的東西:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"Expanded URL = %@", [[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Location"]); 
} 

我想創建一個小URLExpander類親自做這個,有一個簽名是這樣的:

+(void)asyncExpandURL:(NSURL *)aURL didExpandTarget:(id)target selector:(SEL)selector; 

然後就傳回兩個參數在你的消息,一個是短網址,一個是長網址。

+0

我會讓它成爲一個對象,讓它不保留目標。這樣,如果您的對象(例如文檔)消失,您可以釋放對象並從而取消請求。 – 2010-12-19 00:58:26

+0

嗯......似乎沒有返回「位置」標頭: – 2010-12-19 02:00:25

+0

連接:didReceiveResponse:{ \t「Cache-Control」=「max-age = 300,must-revalidate」; \t Connection =「Keep-Alive」; \t「Content-Type」=「text/html; charset = UTF-8」; \t Date =「Sun,19 Dec 2010 01:54:10 GMT」; \t Server =「Apache/2。2「; \t」Set-Cookie「=」X-Mapping-edcdikko = AC17288FCF56ED010476BDE83D0C0E9D;路徑= /「; \t 」傳輸編碼「=身份; \t因人而異= 」接受編碼,曲奇「; \t 」WP-超高速緩存從PHP 「」=「 曾任supercache文件; \t} – 2010-12-19 02:01:02

0

有沒有簡單的方法,你必須要求短網址提供服務器,並獲得完整的網址。這必須通過URL連接完成,也許有一些邏輯背後獲得重定向鏈接(我還沒有嘗試過)

+0

另外,它也不需要完整的GET請求,HEAD應該就足夠了。 – bcarlso 2010-12-19 00:21:16