iOS无法在Mobile Data / 4G网络上上传图片,而在Wifi上运行正常

时间:2018-07-14 07:55:07

标签: ios swift server image-uploading nsurlsession

我正在使用NSURLSesssion上传任务将图像上传到服务器,以字节为单位发送。在wifi中工作正常。但是当我使用MobileData / 4G LTE网络上传时,它显示-1001错误代码。 1.将服务器端超时时间增加到4分钟。 2.尝试使用小kb大小的图像 仍然只有4GLTE网络存在该问题。

注意:相同的服务API在Andriod中可用。

请让我知道会出现什么问题。?

    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    [request setCachePolicy:cachePolicy];
    [self prepareMethod:methodURL methodType:methodType dataInBody:dataInBody contentType:contentType withRequest:request];
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];

    [NSURLRequest allowsAnyHTTPSCertificateForHost:@“yyyy.abc.com"];



    NSMutableData * bodyData = [[NSMutableData alloc] init];

    params = [[[NSUserDefaults standardUserDefaults]objectForKey:@"userparams"]mutableCopy];

        for (NSString *param in params)
        {
            if (![param isEqualToString:@"file"]) {
                [bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
                [bodyData appendData:[[NSString stringWithFormat:@"%@\r\n", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
            }
        }

        // add image data
        [bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpeg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];
        [bodyData appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [bodyData appendData:[params objectForKey:@"file"]];
        [bodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [bodyData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];



    //NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    // sessionConfiguration.timeoutIntervalForRequest = 120.0;
    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionUploadTask* uploadtask = [session uploadTaskWithRequest:request fromData:bodyData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){

              if (!error)
              {
                  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
                  HttpResponse * response = [[HttpResponse alloc] initWithHttpURLResponse:httpResponse withData:data];



                  dispatch_async(dispatch_get_main_queue(), ^{
                      if ([delegate respondsToSelector:@selector(connectionFinishedLoading:)]) {
                          [delegate connectionFinishedLoading:response];
                      }
                  });

              }
              else
              {
                  dispatch_async(dispatch_get_main_queue(), ^{
                      if ([delegate respondsToSelector:@selector(connectionFailedWithError:)])
                      {
                          [delegate connectionFailedWithError:error];
                      }
                  });
              }


          }];
    [uploadtask resume];

}

0 个答案:

没有答案