I am using NSURLConnection to make async HTTPS POST for our web service to download jpg. It works great in most cases.
But when the message body is between 196609 and 196868 bytes (inclusive), the connection freezes after the file has been downloaded 100% (didSendBodyData tells me that it was sent 100%). Then after 5 minutes, the connection time ends with the message "Network connection was lost." Larger and smaller files work. I tested this by closing the size of the file that I am adding to the message body.
Content length is set correctly. See code below.
The download is fine when I download netcat via HTTP. The entire text of the message passes. But I suspect that something went wrong due to SSL encryption, for example, to buffer the encrypted frame of an SSL frame.
Is there an error in the SSL code for the iPhone that causes it not to download the entire body of the message, even if it reports that it or something else?
Here's a section in which I set the length of the content and add the file to the body of a multi-page POST. You can see that I close the files manually to check.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:1];
[request addValue:@"close" forHTTPHeaderField: @"Connection"];
NSLog( @"Posting file." );
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
...
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"uploadFile\"; filename=\"upload.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog( @"file size...%d", [uploadFile length] );
[postBody appendData:[NSData dataWithBytes: [uploadFile bytes] length: 196099]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
[request setValue:[NSString stringWithFormat:@"%d", [postBody length]] forHTTPHeaderField:@"Content-Length"];
NSLog(@"Uploaded POST size: %d", [postBody length] );
source
share