iOS应用程序使用不同的网络提供商连接3G



我正在运行一个iOS应用程序,该应用程序通过3G与某些服务器进行通信。这个服务器接收我们的HTTP请求并处理它们,等等。最近,我们开始注意到,使用不同的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:@"--%@rn", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"rnrn", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@rn", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSError *myError = nil;
NSData *imageData = [NSData dataWithContentsOfFile:zipFilePath];
if (imageData) {
    NSLog(@"Temos imagem!!!!");
    [body appendData:[[NSString stringWithFormat:@"--%@rn", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"; filename="movie.zip"rn", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"rn"] 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网络提供商之间存在哪些差异会导致此问题?

谢谢。

多部分/表单数据消息有两个问题:

  1. 您在严格属于图像数据的图像数据之后添加CRLF。接收者可能会感到困惑(并且可能会感到困惑(。只需省略它 - 对于任何部分,除非服务器混淆了不以 CRLF 结尾的文本类型的正文部分。

  2. 最后一部分(您的图像数据(之后,需要一个闭合边界分隔符。您必须添加一个

    [body appendData:[[NSString stringWithFormat:@"--%@--", BoundaryConstant]
                                 dataUsingEncoding:NSUTF8StringEncoding]];
    

注意:

  1. 您还应该确保第一个边界分隔符以 CRLF 开头(由于 NSURLConnection 在消息头之后添加了 CRLF,这可能已经得到保证(。然而,额外的第一个CRLF不会受到伤害,然后被视为"序幕" - 这没有语义意义。

  2. 严格来说,在结束分隔符之后你不需要 CRLF,但它也不会受到伤害 - 因为这被视为没有语义意义的"尾声"。

相关内容

  • 没有找到相关文章

最新更新