【发布时间】:2014-12-03 22:12:38
【问题描述】:
我编写了以下请求代码(使用 SwiftHTTP 库)
func performTagRequest(detail : String){
var request = HTTPTask()
var formattedDetail = detail.stringByReplacingOccurrencesOfString("\"", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
var url = "www.gifbase.com/tag/\(formattedDetail)?format=json"
url = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
println(url)
request.GET(url, parameters: nil, success: {(response: HTTPResponse) in
if response.responseObject != nil {
let data = response.responseObject as NSData
let str = NSString(data: data, encoding: NSUTF8StringEncoding)!
let gifMetaArray = str.componentsSeparatedByString(",") as [String]
//println("response: \(gifMetaArray))") //prints the HTML of the page
}
},failure: {(error: (NSError, HTTPResponse?)) in
println("error: \(error)")
})
}
在浏览器中返回有效响应 (ex for "10thingsihateaboutyou") 但返回错误
error: (Error Domain=NSURLErrorDomain Code=-1002 "The operation couldn’t be completed.
(NSURLErrorDomain error -1002.)" UserInfo=0x7ff22947b640
{NSErrorFailingURLStringKey=www.gifbase.com/tag/10thingsihateaboutyou?format=json,
NSUnderlyingError=0x7ff22940d550 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork
error -1002.)", NSErrorFailingURLKey=www.gifbase.com/tag/10thingsihateaboutyou?format=json}, nil)
我发现错误 -1002 是 NSURLErrorUnsupportedURL 并且可能是由于未正确转义字符引起的,但我相信我的打印上方的行应该这样做。
有人有这方面的经验吗?
【问题讨论】:
标签: swift nsurlconnection nsurlrequest nsurlsession