【问题标题】:Getting redirected url from instagram server从 instagram 服务器获取重定向的 url
【发布时间】:2015-09-29 05:03:11
【问题描述】:

我正在从 instagram 抓取图像并收到诸如“http://instagram.com/p/1JiWygQUSu/media/?size=l”之类的网址,问题是此网址指向“https://scontent-lax3-1.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/1527608_451589571655110_1239072675_n.jpg”我需要文件扩展名才能在用户设备上正确缓存图像。

第一个 url 重定向到第二个。我需要一种从第一个网址检索第二个网址的方法。

尝试使用此代码,但它只返回原始网址:

let urlString = "http://instagram.com/p/1JiWygQUSu/media/?size=l"
let request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string:urlString)!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: 15.0)
request.HTTPMethod = "HEAD"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
    (response, data, error) in
    print("instaResponse \(response?.URL)")
})

【问题讨论】:

  • 只需从重定向的url中提取lastPathComponent
  • 你是如何抓取这些图像的?你在使用像 AFNetworking 这样的库吗?你在使用 NSURLSession 吗?
  • 我正在使用 nsurlsession

标签: ios swift instagram


【解决方案1】:

试试看NSURLSessionTaskDelegate方法 - URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:Documentation here

您应该能够在那里处理重定向并缓存图像。或者,您可以尝试使用内置图像缓存的SDWebImageAFNetworking,并且可能已经处理了重定向情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 2013-02-24
    • 2010-11-20
    • 2011-03-23
    相关资源
    最近更新 更多