我正在使用 NSURLConnection 对我们的网络服务进行异步 HTTPS POST 以上传 jpg。它在大多数情况下都能正常工作。
但是当帖子正文介于 196609 和 196868 字节(含)之间时,在文件上传 100% 后连接挂起(didSendBodyData 告诉我它已 100% 发送)。然后在 5 分钟后,连接超时并显示消息“网络连接已丢失”。越来越大的文件大小工作。我已经通过限制添加到帖子正文的文件的文件大小来测试这一点。
内容长度设置正确。请参阅下面的代码。
当我通过 HTTP 上传到 netcat 时,上传看起来很好。消息的整个正文都通过了。但我怀疑由于 SSL 加密出了问题,比如它正在缓冲 SSL 加密的块帧。
iPhone 的 SSL 代码中是否存在错误,即使它报告它是,也不会上传整个帖子正文,或者其他什么?
这是我设置内容长度并将文件附加到多部分 POST 正文的部分。您可以看到我正在手动限制文件大小以进行测试。
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:1];
[request addValue:@"close" forHTTPHeaderField: @"Connection"];
// Add headers and POST information
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:uploadFile];
[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] );