[英]Why is my NSURLConnection so slow?
我已使用 Mac OS X參考庫中的“ 使用NSURLConnection”中的指南設置了NSURLConnection,並使用自定義主體將NSMutableURLRequest創建為POST到PHP腳本,以上傳20 MB數據(請參見下面的代碼)。 請注意,帖子正文是與現有桌面實現完全匹配的內容(換行和全部)。
當我在iPhone模擬器中運行此代碼時,發布成功,但是比我在Mac上以C ++在Mac上本地運行等效代碼的時間長了一個數量級(分別為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];
好的,這里有些錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.