Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用不同网络提供商连接3G的iOS应用程序_Ios_Http_Nsurlconnection_Nsurlrequest_3g - Fatal编程技术网

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

使用不同网络提供商连接3G的iOS应用程序,ios,http,nsurlconnection,nsurlrequest,3g,Ios,Http,Nsurlconnection,Nsurlrequest,3g,我正在运行一个iOS应用程序,它通过3G与某个服务器通信。这个服务器接收我们的HTTP请求并处理它们,bla-bla-bla。 最近,我们开始注意到,使用不同的3g提供商与服务器通信,结果是截然不同的 例如,在我们的一个案例中,我们尝试使用以下方法上载zip文件: + (void) UploadZipImages:(NSString*)zipFilePath delegate:(UIViewController *)_delegate{ isLastUploadLocal = false;

我正在运行一个iOS应用程序,它通过3G与某个服务器通信。这个服务器接收我们的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网络提供商之间存在哪些差异可能导致这些问题


谢谢。

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

  • 您正在图像数据之后添加一个CRLF,该图像数据严格属于图像数据。接收者可能会感到困惑(而且可能会)。只需忽略这一点——对于任何部分,除非服务器在文本类型的主体部分(不以CRLF结尾)中感到困惑

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

    [body appendData:[[NSString stringWithFormat:@"--%@--", BoundaryConstant]
                                 dataUsingEncoding:NSUTF8StringEncoding]];
    
  • 注:
  • 您还应该确保第一个边界分隔符以CRLF开头(这可能已经得到了保证,因为NSURLConnection在消息头之后添加了CRLF)。然而,一个附加的第一个CRLF不会造成伤害,然后被视为“开场白”——没有语义意义

  • 严格地说,您不需要在结束分隔符后加上CRLF,但这也不会有什么坏处——因为这被视为“尾声”,没有任何语义意义