|
|
@@ -510,7 +510,7 @@ repeat:
|
|
|
return session;
|
|
|
}
|
|
|
|
|
|
-+ (NSURLSessionDataTask *)downloadFileOffset:(NSUInteger)offset Param:(NSDictionary *)param from:(NSString *)url method:(NSString *)method toPath:(NSString *)path progressHandler:(progressHandler)progressHandler completionHandler:(resultHandler)result {
|
|
|
++ (NSURLSessionDownloadTask *)downloadFileOffset:(NSUInteger)offset Param:(NSDictionary *)param from:(NSString *)url method:(NSString *)method toPath:(NSString *)path progressHandler:(progressHandler)progressHandler completionHandler:(resultHandler)result {
|
|
|
|
|
|
NSURLSession *sessoin = [self standSession];
|
|
|
|
|
|
@@ -544,7 +544,8 @@ repeat:
|
|
|
req.HTTPBody = [paramStr dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
}
|
|
|
|
|
|
- NSURLSessionDataTask *downloadTask = [sessoin dataTaskWithRequest:req];
|
|
|
+
|
|
|
+ NSURLSessionDownloadTask *downloadTask = [sessoin downloadTaskWithRequest:req];
|
|
|
|
|
|
[downloadTask resume];
|
|
|
|
|
|
@@ -1279,7 +1280,7 @@ repeat:
|
|
|
|
|
|
|
|
|
|
|
|
-+(void)request_file : (NSString*) url parameters:(NSMutableDictionary *) params err_record_url:(NSString* )recorder_url completionHandler:(resultHandler)result
|
|
|
++(void)request_file_1 : (NSString*) url parameters:(NSMutableDictionary *) params err_record_url:(NSString* )recorder_url completionHandler:(resultHandler)result
|
|
|
/* dataHandler:(dataHandler)returndata*/
|
|
|
{
|
|
|
// __block int retry = 0;
|
|
|
@@ -1686,4 +1687,123 @@ repeat:
|
|
|
// return responseData;
|
|
|
// }
|
|
|
}
|
|
|
++(void)request_file_fast : (NSString*) url parameters:(NSMutableDictionary *) params completionHandler:(resultHandler)result
|
|
|
+
|
|
|
+
|
|
|
+// Param:(NSDictionary *)param from:(NSString *)url completionHandler:(resultHandler)result
|
|
|
+/* dataHandler:(dataHandler)returndata*/
|
|
|
+{
|
|
|
+
|
|
|
+ params = [self prepare_addtional_params:params];
|
|
|
+
|
|
|
+ NSURLSession *urlSession = [self standSession];
|
|
|
+
|
|
|
+// RANetworkTaskDelegate *delegate = (RANetworkTaskDelegate *)urlSession.delegate;
|
|
|
+// delegate.downloadTask = YES;
|
|
|
+// delegate.fileCachePath = path;
|
|
|
+// if (progressHandler) {
|
|
|
+// delegate.p = progressHandler;
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (result) {
|
|
|
+// delegate.r = result;
|
|
|
+// }
|
|
|
+
|
|
|
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ request.HTTPMethod = @"Post";
|
|
|
+//
|
|
|
+// if (offset > 0) {
|
|
|
+// [request addValue:[NSString stringWithFormat:@"bytes=%ld-",(unsigned long)offset] forHTTPHeaderField:@"Range"];
|
|
|
+// }
|
|
|
+
|
|
|
+ // 拼接参数
|
|
|
+ if (params && params.allValues.count > 0) {
|
|
|
+ __block NSMutableArray *paramArr = [NSMutableArray array];
|
|
|
+ [params enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
|
|
|
+ [paramArr addObject:[NSString stringWithFormat:@"%@=%@",key,obj]];
|
|
|
+ }];
|
|
|
+ NSString *paramStr = [paramArr componentsJoinedByString:@"&"];
|
|
|
+
|
|
|
+ request.HTTPBody = [paramStr dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ NSURLSessionDownloadTask* downloadTask=[urlSession downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ NSHTTPURLResponse* urlResponse =response;
|
|
|
+ NSData* data =[NSData dataWithContentsOfURL:location] ;
|
|
|
+ NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
|
|
|
+ if(data==nil)
|
|
|
+ {
|
|
|
+ NSString* err_msg = [error localizedDescription];
|
|
|
+ NSDictionary* error_json = [RAUtils error_dict:error];
|
|
|
+ [ret setObject:error_json forKey:@"err_obj"];
|
|
|
+ NSString* resp_msg= nil;
|
|
|
+
|
|
|
+ if(err_msg.length==0)
|
|
|
+ {
|
|
|
+ err_msg =[NSHTTPURLResponse localizedStringForStatusCode:urlResponse.statusCode];
|
|
|
+ resp_msg = err_msg;
|
|
|
+ }
|
|
|
+ [ret setValue:[NSString stringWithFormat:@"%d",RESULT_NET_ERROR] forKey:@"result"];
|
|
|
+ [ret setValue:err_msg forKey:@"err_msg"];
|
|
|
+ [ret setValue:resp_msg forKey:@"resp_msg"];
|
|
|
+ [ret setValue:[NSString stringWithFormat:@"%ld",(long)urlResponse.statusCode] forKey:@"resp_code"];
|
|
|
+
|
|
|
+ dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
+ if(result)
|
|
|
+ result(ret);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ NSString* path=NSTemporaryDirectory();
|
|
|
+
|
|
|
+ if (response.suggestedFilename) {
|
|
|
+ path = [path stringByAppendingPathComponent:response.suggestedFilename];
|
|
|
+ } else {
|
|
|
+ path = [path stringByAppendingPathComponent:[NSUUID UUID].UUIDString];
|
|
|
+ }
|
|
|
+ NSError *error = nil;
|
|
|
+
|
|
|
+ [[NSFileManager defaultManager] copyItemAtPath:location.path toPath:path error:&error];
|
|
|
+
|
|
|
+// [[NSFileManager defaultManager] copyItemAtURL:[NSURL URLWithString:location.path] toURL:[NSURL URLWithString:path] error:&error];
|
|
|
+// NSFileManager
|
|
|
+// NSString *cacheDir = NSTemporaryDirectory();
|
|
|
+//
|
|
|
+// if (![[NSFileManager defaultManager] fileExistsAtPath:cacheDir]) {
|
|
|
+// [[NSFileManager defaultManager] createDirectoryAtPath:cacheDir withIntermediateDirectories:NO attributes:nil error:nil];
|
|
|
+// }
|
|
|
+
|
|
|
+ ret[@"result"]=@"2";
|
|
|
+ ret[@"data"]=data;
|
|
|
+ ret[@"path"]=path;//[location absoluteString];
|
|
|
+ dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
+ if(result)
|
|
|
+ result(ret);
|
|
|
+ });
|
|
|
+ }
|
|
|
+// if(returndata)
|
|
|
+// {
|
|
|
+// returndata(data);
|
|
|
+// }
|
|
|
+ }];
|
|
|
+
|
|
|
+ [downloadTask resume];
|
|
|
+
|
|
|
+ return ;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
@end
|