[問題] 利用curl 判斷是檔案還是網頁

看板C_and_CPP (C/C++)作者 (哭哭)時間10年前 (2016/03/22 14:59), 編輯推噓0(003)
留言3則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) ubuntu 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) libcurl 問題(Question): 小弟我是C的新手,最近想要用curl來下載遠端的檔案 但想要判斷使用者輸入的是網頁還是檔案 如果是網頁則程式結束 否則就下載檔案 其他資訊如下, 餵入的資料(Input): (1) www.google.com.tw (2) https://curl.haxx.se/download/curl-7.47.1.tar.bz2 程式碼(Code):(請善用置底文網頁, 記得排版) 源自:http://stackoverflow.com/questions/1636333/download-file-using-libcurl-in-c-c [B #include <stdio.h> #include <stdlib.h> #include <curl/curl.h> int main(void) { CURL *curl; FILE *fp; CURLcode res; //char *url = "www.google.com.tw"; //(1) char *url = "https://curl.haxx.se/download/curl-7.47.1.tar.bz2";//(2) /////////////////////////////////////// /*假設是(1) 則不下載,若是(2),則下載*/ /////////////////////////////////////// char outfilename[100] = "output.tar.bz2"; curl = curl_easy_init(); if (curl) { fp = fopen(outfilename,"wb"); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_cleanup(curl); fclose(fp); } return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.140.82.1 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1458629994.A.056.html

03/22 15:06, , 1F
載下來再判斷會不會比較容易
03/22 15:06, 1F

03/22 16:52, , 2F
根據Content-Type判斷是否為application/octet-stream
03/22 16:52, 2F

03/22 16:52, , 3F
這樣不知道是否可行
03/22 16:52, 3F
文章代碼(AID): #1MyErg1M (C_and_CPP)
文章代碼(AID): #1MyErg1M (C_and_CPP)