1
votes

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
  • 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
Content-Length: 96

<html><head><title>POST RESPONSE</title></head><body><p>The file was uploaded.</p></body></html>
  • iOS application receives response and NSLogs the response via NSURLSessionTaskDelegate -> URLSession:task:didCompleteWithError:
<NSHTTPURLResponse: 0x15d95110> { URL: http://10.157.239.129:42000/ } { status code: 200, headers {
    "Content-Length" = 96;
    "Content-Type" = "text/html";
    Date = "Fri, 30 Oct 2015 16:15:21 GMT";
    Server = "BaseHTTP/0.3 Python/2.7.10";
} }
  • however, when I try to NSLog the response content when the method NSURLSessionTaskDelegate: -> URLSession:dataTask:didReceiveData:
- (void)URLSession:(NSURLSession *)session
          dataTask:(NSURLSessionDataTask *)dataTask
    didReceiveData:(NSData *)data
{
    // Called when data task has received some data (not applicable for straight upload?)
    if (data != NULL)
    {
        NSLog(@"%s: %@", __PRETTY_FUNCTION__,[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }
    else
    {
        NSLog(@"%s: but no actual data received.", __PRETTY_FUNCTION__);
    }

    // If we are not in the "active" or foreground then log some background information to the console
    if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive)
    {
        [BackgroundTimeRemainingUtility NSLog];
    }
}
  • is called I get this for output:
2015-10-30 12:15:22.648 NSURLSessionUploadTaskExample[215:7286] -[ViewController URLSession:dataTask:didReceiveData:]: (null)
  • what is odd about this is that the response should not have triggered the if statement block if the data is null!
  • I also have evidence that the response data WAS sent to the iOS application via Wireshark. Here's a packet capture of the HTTP response from the server: Wireshark

Can anyone tell me why iOS appears to be losing the content in the HTTP response?

1
Can you please look into this stackoverflow.com/q/38831704/5730180 ?RajuBhai Rocker

1 Answers

0
votes

Because I was able to trace the HTTP 200 response from the server to the iOS application, I suspected the iOS application had a problem. I set a breakpoint at the line:

NSLog(@"%s: %@", __PRETTY_FUNCTION__,[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

and examined the data object with the debugger:

enter image description here

Each of the 96 bytes in the response content was getting to NSURLSessionTaskDelegate: -> URLSession:dataTask:didReceiveData: So the NSString decoding in the NSLog was at fault. Now things were making sense. I was sending a 96 byte ASCII response, not a 96 byte UTF8 response!

I replaced the NSString decoder with the following:

NSLog(@"%s: %@", __PRETTY_FUNCTION__,[[NSString alloc]  initWithBytes:[data bytes] length:[data length] encoding: NSASCIIStringEncoding]);

Problem solved with the following output:

2015-11-02 10:04:47.086 NSURLSessionUploadTaskExample[561:363449] -[ViewController URLSession:dataTask:didReceiveData:]:<html><head><title>POST RESPONSE</title></head><body><p>The file was uploaded.</p></body></html>

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