ios 下使用curl openssl 下載http或者https檔案
阿新 • • 發佈:2018-12-18
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
curl_easy_setopt(curl, CURLOPT_RANGE, "0-500000");//用於斷點續傳, 設定下載的分片
這裡寫的是 獲取0-500k 之間的資料 如果要取別的 可以寫200-5000 就是200 到5000位元組的資料
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
這個使用者獲取請求的總時長,對獲取總時長有幫助寫在curl_easy_perform
後面
if(res == CURLE_OK){ double resulT =0; res = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &resulT); if(CURLE_OK == res) { printf("Time: %f", resulT); } }
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
為了保證移動端相容性的使用
不能使用ios的urlsession
可以使用C語言的curl庫來實現下載操作
curl在ios環境下的編譯
因為之前編譯過openssl 就直接拿過來用了
openssl 是為了增加https支援來使用的
編譯通過後使用就非常簡單了
使用程式碼
// // ViewController.m // libcurlHTTPS // // Created by kai_leedarson on 2018/10/29. // Copyright © 2018年 maple_leedarson. All rights reserved. // #import "ViewController.h" #include <curl/curl.h> #import "JsonDictExchange.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. curl_version_info_data *data= curl_version_info(CURLVERSION_NOW); printf("\nopenssl version %s\n",data->ssl_version); } static int ProgressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) { static int pre_percent =0; if ( dltotal > -0.1 && dltotal < 0.1 ) return 0; int nPos = (int) ( (dlnow/dltotal)*100 ); pre_percent = nPos; NSLog(@"%d%%",pre_percent); return 0; } bool getUrl(const char *filename,char *url) { CURL *curl; CURLcode res; FILE *fp; if ((fp = fopen(filename, "wt+")) == NULL){ // 返回結果用檔案儲存 return false; } struct curl_slist *headers = NULL; //增加HTTP header // headers = curl_slist_append(headers, "Accept:application/json"); // headers = curl_slist_append(headers, "Content-Type:application/json"); // headers = curl_slist_append(headers, "charset:utf-8"); curl = curl_easy_init(); // 初始化 if (curl) { // curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改協議頭 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); curl_easy_setopt(curl, CURLOPT_URL,url); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, 0L); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, ProgressCallback); curl_easy_setopt(curl, CURLOPT_RANGE, "0-5000");//用於斷點續傳, 設定下載的分片 NSLog(@"請求"); res = curl_easy_perform(curl); // 執行 NSLog(@"請求返回值是%i",res); curl_slist_free_all(headers); curl_easy_cleanup(curl); } fclose(fp); return true; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self testWeb]; } - (void)testWeb { NSString * path0 = [[NSBundle bundleForClass:[self class]] pathForResource:[NSString stringWithUTF8String:"testCloud.json"] ofType:nil]; NSData *receiveData = [[NSData alloc] initWithContentsOfFile:path0]; //獲得的json先轉換成字串 NSString *receiveStr = [[NSString alloc]initWithData:receiveData encoding:NSUTF8StringEncoding]; //再解析 NSDictionary *jsonDict = [JsonDictExchange convertJsonWith:receiveStr]; NSMutableArray *dataArray = [jsonDict[@"data"] mutableCopy]; for (NSInteger i=0; i<dataArray.count; i++) { NSMutableDictionary *info = [dataArray[i] mutableCopy]; NSLog(@"開始下載[%ld]url\n%@",(long)i,info[@"url"]); NSString *downloadPath = info[@"url"]; NSArray *array = [info[@"url"] componentsSeparatedByString:@"/"]; NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:array.lastObject]; //6.3 執行剪下操作 char *charPath1 = (char *)[downloadPath UTF8String]; char *charPath2 = (char *)[fullPath UTF8String]; getUrl(charPath2,charPath1); } } @end
專案結構,記得去掉bitcode 為no 並且加個libz的庫
下載效果圖片
我只編譯了armv7 和arm64的真機版本,需要其他的只能自己編譯了
目前還存在沒有使用多執行緒下載來提速的問題。