URL下载

let path = "http://api.tuicool.com/api/articles/BriMni2.json?is_pad=1&need_image_meta=1"
        let url = NSURL(string: path)
        if let u = url {
//            let task = NSURLSession.sharedSession().dataTaskWithURL(u, completionHandler: { (data, response, error) in
//                print("\(response)")
//            })
//            task.resume()
            
            let request = NSMutableURLRequest(URL: u)
            request.addValue("Basic MC4wLjAuMDp0dWljb29s", forHTTPHeaderField: "Authorization")
            let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) in
                
//                let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
//                print(str)
                
                let jsonObj = try! NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
                print(jsonObj)
            })
            task.resume()
        }

你可能感兴趣的:(URL下载)