在 Cocoa 中获取短 URL 的长 URL 的最简单方法?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新:我刚刚看到您的评论并意识到它正在遵循重定向。
请参阅委托方法:
connection:willSendRequest:redirectResponse:
,它告诉您它正在根据之前的响应重定向到此新请求。您可以从此处的新请求或重定向响应的 Location 标头获取扩展的 URL。
原始答案如下...
将
NSURLConnection
与委托一起使用。在委托的connection:didReceiveResponse:
方法中,获取allHeaderFields
并读取“Location”标头的值。类似于:
我会创建一个小的
URLExpander
类来亲自执行此操作,其签名类似于:然后在消息中传回两个参数,一个用于短 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.
Original answer follows...
Use
NSURLConnection
with a delegate. In your delegate'sconnection:didReceiveResponse:
method, fetchallHeaderFields
and read the value of the "Location" header.Something like:
I'd create a little
URLExpander
class to do this personally, with a signature something like:Then just pass back two arguments in your message, one for the short URL, one for the long.
没有简单的方法,您必须请求短网址提供服务器并获取完整的网址。这必须通过 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)
如果您不想发出请求并遵循重定向,更简单的方法是使用链接延长服务。以下是两个简单的 API:
http://longurl.org/api/#expand-url< /a>
http://www.longurlplease.com/docs
我可能很快就会发布一些代码片段,特别是如果有兴趣的话。
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:
http://longurl.org/api/#expand-url
http://www.longurlplease.com/docs
I may post some code snippets soon especially if there is interest.