我正在运行一个通过 3G 与某个服务器通信的 iOS 应用程序。这个服务器接收我们的 HTTP 请求并处理它们,bla bla bla。最近,我们开始注意到使用不同的 3g 提供商与服务器通信,结果是相当不同的。
例如,在我们的一个案例中,我们尝试使用这种方法上传一个 zip 文件:
+ (void) UploadZipImages:(NSString*)zipFilePath delegate:(UIViewController *)_delegate{
isLastUploadLocal = false;
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:@"yyyyMMddhhmmss"];
NSString *dateString = [dateFormatter stringFromDate:date];
// Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept.
NSString *title = [NSString stringWithFormat:@"%@.zip", dateString];
// the boundary string : a random string, that will not repeat in post data, to separate post data fields.
NSString *BoundaryConstant = @"----WebKitFormBoundary5FyPE45e6sSDdGnYP";
// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
NSString* FileParamConstant = @"file";
// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:@"http://*************"];
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:[NSString stringWithFormat:@"%@",title] forKey:@"title"];
NSMutableDictionary* _params2 = [[NSMutableDictionary alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:1200];
[request setAllowsCellularAccess:YES];
[request setHTTPMethod:@"POST"];
[request setNetworkServiceType:NSURLNetworkServiceTypeDefault];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in _params) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSError *myError = nil;
NSData *imageData = [NSData dataWithContentsOfFile:zipFilePath];
if (imageData) {
NSLog(@"Temos imagem!!!!");
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"movie.zip\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}else{
NSLog(@"There was an error %@", myError);
}
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
[request setURL:requestURL];
[[NSURLConnection alloc] initWithRequest:request delegate:_delegate];
}
这在使用某些 3G 互联网提供商时可以正常工作(而且速度相当快),但对于其他运营商来说,请求会永远开始处理,直到达到超时间隔...... 3G 网络提供商之间可能存在哪些可能导致此问题的差异?
谢谢你。