我使用URLSessionConfiguration.background和uploadTask从iOS应用程序上传文件。
上载会话的配置方式如下:
let configuration = URLSessionConfiguration.background(withIdentifier: "com.mycompany.myapp.fileUploader")
configuration.isDiscretionary = false
configuration.allowsCellularAccess = true
uploadURLSession = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)请求是:
request.httpMethod = "POST"
request.setValue("application/octect-stream", forHTTPHeaderField: "Content-Type")
let task = uploadURLSession.uploadTask(with: request, fromFile: fileURL)我想了解如何管理错误处理。
URLSession.uploadTask如何处理http错误4xx或5xx?
如何触发对5xx错误的重试?
发布于 2022-06-01 14:59:48
URLSession.uploadTask不处理http服务器端错误。它只处理客户端或网络问题的重试和错误。
必须从任务响应中检索http状态/错误,将其转换为HTTPURLResponse。
直接来自uploadTask调用(背景URLSession不支持):
let task = uploadSession?.uploadTask(with: request, fromFile: fileURL, completionHandler: { data, response, error in
if let error = error {
print("Upload error:\(error)")
//Client side error
return
}
guard let res = response as? HTTPURLResponse else {
print("Upload completed with response:\(response.description ?? "undefined")")
//It should not happen at all
return
}
if (200...299).contains(res.statusCode) {
print("Upload completed successfully. Status code:\(res.statusCode)")
}
else if (400...499).contains(res.statusCode) {
print("Upload fatal issue. Status code:\(res.statusCode)")
//Fatal issue, do not retry the upload
}
else if (500...599).contains(res.statusCode) {
print("Upload issue. Status code:\(res.statusCode)")
//Schedules a new uploading task for the file
}
else {
print("Upload completed with status code:\(res.statusCode)")
}
})或者,如果您使用的是背景URLSession,则来自URLSessionTaskDelegate:
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
print("Upload error:\(error)")
//Client side error
return
}
guard let res = task.response as? HTTPURLResponse else {
print("Upload completed with response:\(task.response?.description ?? "undefined")")
//It should not happen at all
return
}
if (200...299).contains(res.statusCode) {
print("Upload completed successfully. Status code:\(res.statusCode)")
}
else if (400...499).contains(res.statusCode) {
print("Upload fatal issue. Status code:\(res.statusCode)")
//Fatal issue, do not retry the upload
}
else if (500...599).contains(res.statusCode) {
print("Upload issue. Status code:\(res.statusCode)")
//Schedules a new uploading task for the file
}
else {
print("Upload completed with status code:\(res.statusCode)")
}
}发布于 2022-05-25 09:26:41
创建这样的上传任务:
let task = uploadSession?.uploadTask(with: request, fromFile: fileURL, completionHandler: { data, response, error in
if let error = error {
print("Upload error: \(error.localizedDescription)")
} else {
if let response = response as? HTTPURLResponse {
print("Response status code: \(response.statusCode)")
}
}
})
task?.resume()您可以处理回调URLSessionTaskDelegate中的错误和responsecode,或者可以查看URLSessionDataDelegate和☝️,以获得对上传任务的细粒度控制。
https://stackoverflow.com/questions/72374503
复制相似问题