0

Here's where I am so far with NSURLSessionUploadTask:

  • iOS application starts an NSURLSessionUploadTask using POST
  • server receives HTTP POST request
  • server reads content of the request so data is uploaded
  • iOS application calls URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend: repeatedly ending with a final call with totalBytesSent equal to totalBytesExpectedToSend
  • server sends HTTP response to iOS consisting of the following:
HTTP/1.1 200 OK
Server: BaseHTTP/0.3 Python/2.7.10
Date: Fri, 30 Oct 2015 16:15:21 GMT
Content-Type: text/html

<html><head><title>POST RESPONSE</title></head><body><p>The file was uploaded.</p></body></html>
  • iOS application receives this response (confirmed via Wireshark) Wireshark
  • iOS application does not call the following methods to complete the upload task like it supposed to:
    • NSURLSessionTaskDelegate: -> URLSession:dataTask:didReceiveData:
    • NSURLSessionTaskDelegate -> URLSession:task:didCompleteWithError:
  • instead after hanging for ~200 seconds, the iOS application repeats the data upload task

This data upload, HTTP 200 response, ~200 second hang process appears to repeat indefinitely. Why aren't NSURLSessionTaskDelegate: -> URLSession:dataTask:didReceiveData: and NSURLSessionTaskDelegate -> URLSession:task:didCompleteWithError: being called once the HTTP response is received by the iOS application?

drbobdugan
  • 443
  • 5
  • 12

1 Answers1

0

After correctly uploading the data via an HTTP request to the server, the iOS application received the HTTP response, but because there was no Content-Length transmitted in the HTTP response, the iOS application had no way of knowing when the HTTP response had ended. Two possible solutions:

  • server closes socket after HTTP response is transmitted
  • Content-Length is included in HTTP response
HTTP/1.1 200 OK
Server: BaseHTTP/0.3 Python/2.7.10
Date: Fri, 30 Oct 2015 16:15:21 GMT
Content-Type: text/html
Content-Length: 96

<html><head><title>POST RESPONSE</title></head><body><p>The file was uploaded.</p></body></html>

I tried both solutions sucessfully, but the Content-Length solution is superior because it reduces network overhead by allowing iOS application and server (assuming the server supports keep-alive) to maintain the socket connection for future HTTP requests.

I've also put a very basic NSURLSessionUploadTask example application and webserver on github here.

drbobdugan
  • 443
  • 5
  • 12