我已经使用Mac OS X参考库中Using NSURLConnection中的指南设置了NSURLConnection,创建了一个NSMutableURLRequest作为POST脚本,使用自定义正文来上传20 MB数据(参见下面的代码)。请注意,帖子正文就是它(换行符和所有)与现有桌面实现完全匹配。
当我在iPhone模拟器中运行时,帖子成功,但比我在Mac上用C ++本地运行等效代码(分别为20分钟对20秒)要长一个数量级。
为什么差异如此戏剧性的任何想法?我知道模拟器将提供与实际设备不同的结果,但我预计至少会有类似的结果。
由于
const NSUInteger kDataSizePOST = 20971520;
const NSString* kUploadURL = @"http://WWW.SOMEURL.COM/php/test/upload.php";
const NSString* kUploadFilename = @"test.data";
const NSString* kUsername = @"testuser";
const NSString* kHostname = @"testhost";
srandom(time(NULL));
NSMutableData *uniqueData = [[NSMutableData alloc] initWithCapacity:kDataSizePOST];
for (unsigned int i = 0 ; i < kDataSizePOST ; ++i) {
u_int32_t randomByte = ((random() % 95) + 32);
[uniqueData appendBytes:(void*)&randomByte length:1];
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:kUploadURL]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"aBcDd";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"--%@\nContent-Size:%d",boundary,[uniqueData length]] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\nContent-Disposition: form-data; name=test; filename=%@", kUploadFilename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@";\nContent-Type: multipart/mixed;\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:uniqueData]];
[postbody appendData:[[NSString stringWithFormat:@"--%@\nContent-Size:%d",boundary,[kUsername length]] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\nContent-Disposition: inline; name=Username;\n\r\n%@",kUsername] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"--%@\nContent-Size:%d",boundary,[kHostname length]] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\nContent-Disposition: inline; name=Hostname;\n\r\n%@",kHostname] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\n--%@--",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
_receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
[request release];
[uniqueData release];
答案 0 :(得分:1)