在 Cocoa 中获取短 URL 的长 URL 的最简单方法?

发布于 2024-10-08 06:42:48 字数 59 浏览 5 评论 0原文

在 Cocoa 中检索短 URL 的原始 URL 最简单的方法是什么?有什么事情可以只用几行代码完成吗?

What is the simplest way to retrieve the original URL for a short URL in Cocoa? Anything that can be done in just a few lines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女中豪杰 2024-10-15 06:42:48

更新:我刚刚看到您的评论并意识到它正在遵循重定向。

请参阅委托方法:connection:willSendRequest:redirectResponse:,它告诉您它正在根据之前的响应重定向到此新请求。

您可以从此处的新请求或重定向响应的 Location 标头获取扩展的 URL。

讨论如果代表希望
取消重定向,它应该调用
连接对象的取消方法。
或者,委托方法可以
返回 nil 取消重定向,并且
连接将继续
过程。这具有特殊的相关性
没有redirectResponse的情况
零。在这种情况下,任何数据
将发送已加载的连接
给代表,代表将
接收连接DidFinishLoading
或连接:didFailLoadingWithError:
消息,视情况而定。

原始答案如下...

NSURLConnection 与委托一起使用。在委托的 connection:didReceiveResponse: 方法中,获取 allHeaderFields 并读取“Location”标头的值。

类似于:

-(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;

然后在消息中传回两个参数,一个用于短 URL,一个用于长 URL。

UPDATE: I just saw your comment and realised it's following the redirect.

See the delegate method: connection:willSendRequest:redirectResponse:, which tells you it's doing a redirect to this new request, based on the previous response.

You can get the expanded URL either from the new request here, or from the Location header of the redirect response.

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.

Original answer follows...

Use NSURLConnection with a delegate. In your delegate's connection:didReceiveResponse: method, fetch allHeaderFields and read the value of the "Location" header.

Something like:

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

I'd create a little URLExpander class to do this personally, with a signature something like:

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

Then just pass back two arguments in your message, one for the short URL, one for the long.

秋日私语 2024-10-15 06:42:48

没有简单的方法,您必须请求短网址提供服务器并获取完整的网址。这必须通过 url 连接来完成,并且可能需要一些逻辑来获取重定向链接(我还没有尝试过)

there is no simple way, you have to request the short-url-providing-server and get the full url. This must be done with a url connection and maybe some logic behind to get the redirect link (I haven't tried yet)

终弃我 2024-10-15 06:42:48

如果您不想发出请求并遵循重定向,更简单的方法是使用链接延长服务。以下是两个简单的 API:

我可能很快就会发布一些代码片段,特别是如果有兴趣的话。

If you don't want to make the request and follow the redirects, a simpler way is to use a link-lengthening service. Here are two simple APIs:

I may post some code snippets soon especially if there is interest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文