首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOS代销商链接缩短,bit.ly不起作用

IOS代销商链接缩短,bit.ly不起作用
EN

Stack Overflow用户
提问于 2012-02-23 05:26:01
回答 2查看 1.7K关注 0票数 0

我想用bit.ly来追踪我的itunes会员链接。我从http://target.georiot.com获得会员链接。当不使用直接链接(转到itunes)时,它会起作用。但是当我用bitly缩短会员链接时,它并不在同一页上。

下面是获取缩短url的代码:

代码语言:javascript
复制
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/....

有什么想法吗?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-23 05:36:25

在将longURL作为查询字符串发送到bit.ly之前,您可能需要对其进行URL编码

您可以使用NSString方法stringByAddingPercentEscapesUsingEncoding:

代码语言:javascript
复制
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];
票数 0
EN

Stack Overflow用户

发布于 2012-03-04 10:51:14

我刚刚尝试使用bit.ly REST API创建一个简短的url,返回的URL按预期工作,如下所示。它看起来像先前的答案,表明编码是在目标上,而标准的url编码(百分比编码,如http://meyerweb.com/eric/tools/dencoder/)似乎可以做到这一点。

此调用(使用正确的API密钥):https://api-ssl.bitly.com/v3/shorten?login=georiot&apiKey=R_MY_API_KEY_HERE&longUrl=http%3A%2F%2Ftarget.georiot.com%2FProxy.ashx%3Fgrid%3D64%26id%3D8i%2FET44NjHw%26offerid%3D146261%26type%3D3%26subid%3D0%26tmpid%3D1826%26RD_PARM1%3Dhttp%3A%2F%2Fitunes.apple.com%2Fus%2Falbum%2Fmetallica%2Fid278116714%3Fuo%3D4%26partnerId%3D30%2F&format=json

返回:{ "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之间的额外重定向,大大加快了用户的响应时间。

无论如何,距离最初的帖子已经有一段时间了,所以希望你能弄清楚这一点。如果没有,请让我们知道,我们将尽我们所能提供帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9403282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档