我想用bit.ly来追踪我的itunes会员链接。我从http://target.georiot.com获得会员链接。当不使用直接链接(转到itunes)时,它会起作用。但是当我用bitly缩短会员链接时,它并不在同一页上。
下面是获取缩短url的代码:
NSString *longURL = link;
NSString *bitlyRequestURLString = [NSString stringWithFormat:@"http://api.bit.ly/shorten?version=2.0.1&format=xml&login=%@&apiKey=%@&longUrl=%@",
@"myappname",
@"myappidentifier",
longURL];
NSURL *bitlyURL = [NSURL URLWithString:bitlyRequestURLString];
// get the short URL from bit.ly
NSError *error;
NSString *response = [NSString stringWithContentsOfURL:bitlyURL encoding:NSUTF8StringEncoding error:&error];
NSString *shortURL = @"";
NSArray *responseParts = [response componentsSeparatedByString:@"<shortUrl>"];
if ([responseParts count] > 1) {
NSString *responsePart = [responseParts objectAtIndex:1];
responseParts = [responsePart componentsSeparatedByString:@"</shortUrl>"];
if ([responseParts count] > 0) {
shortURL = [responseParts objectAtIndex:0];
}
}最后一个重定向链接类似于"http://phobos.apple.com/WebObjects/....“
有什么想法吗?谢谢
发布于 2012-02-23 05:36:25
在将longURL作为查询字符串发送到bit.ly之前,您可能需要对其进行URL编码
您可以使用NSString方法stringByAddingPercentEscapesUsingEncoding:
NSString *longURL = [link stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
NSString *bitlyRequestURLString = [NSString stringWithFormat:@"http://api.bit.ly/shorten?version=2.0.1&format=xml&login=%@&apiKey=%@&longUrl=%@",
@"myappname",
@"myappidentifier",
longURL];发布于 2012-03-04 10:51:14
我刚刚尝试使用bit.ly REST API创建一个简短的url,返回的URL按预期工作,如下所示。它看起来像先前的答案,表明编码是在目标上,而标准的url编码(百分比编码,如http://meyerweb.com/eric/tools/dencoder/)似乎可以做到这一点。
返回:{ "status_code":200,"status_txt":"OK","data":{ "long_url":"http://target.georiot.com/Proxy.ashx?grid=64&id=8i/ET44NjHw&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=http://itunes.apple.com/us/album/metallica/id278116714?uo=4&partnerId=30/","url":"http://bit.ly/zR6uzb","hash":"zR6uzb","global_hash":"wFpgG2","new_hash":1}}
结果url按预期工作(在删除转义/之后):http:\bit.ly\zR6uzb
在GeoRiot,我们最近也添加了一个新的集成url缩短程序,这可能会让你感兴趣,但是我们还没有公开它的API。如果你有兴趣在我们有机会的时候尝试一下,请让我们知道。这里最大的好处是消除了bit.ly和georiot之间的额外重定向,大大加快了用户的响应时间。
无论如何,距离最初的帖子已经有一段时间了,所以希望你能弄清楚这一点。如果没有,请让我们知道,我们将尽我们所能提供帮助!
https://stackoverflow.com/questions/9403282
复制相似问题