简体   繁体   English

使用自定义委托从NSURLSessionUploadTask获取服务器响应

[英]Get server response from NSURLSessionUploadTask using custom delegate

I would like to upload an image to a server by using NSURLSessionUploadTask , which I have already accomplished by using a custom delegate 我想使用NSURLSessionUploadTask将图像上传到服务器,这已经通过使用自定义委托完成

let task =  NSURLSession(
    configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
    delegate: self,
    delegateQueue: NSOperationQueue.mainQueue()
).uploadTaskWithRequest(request, fromData: data)

task!.resume()

But I am unable to get the server's response. 但是我无法获得服务器的响应。 Using task.response returns the error code and other header information, but does not return the actual html shown on the page. 使用task.response返回错误代码和其他标头信息,但不返回页面上显示的实际html。

I can't create the url task with a completion handler, because I need to get the progress of the upload. 我无法使用完成处理程序创建url任务,因为我需要获取上传进度。

How can I get the NSData from a page response using NSURLSessionUploadTask ? 如何使用NSURLSessionUploadTask从页面响应中获取NSData?

You can gather the data via didReceiveData , and could have a global NSData instance and append the received data. 您可以通过didReceiveData收集数据,并且可以具有全局NSData实例并追加接收到的数据。

Then, in didCompleteWithError , you can convert the received data to a string to get the response HTML. 然后,在didCompleteWithError ,您可以将接收到的数据转换为字符串以获取响应HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM