作者justfor0223 (哭哭)
看板C_and_CPP
标题[问题] 利用curl 判断是档案还是网页
时间Tue Mar 22 14:59:49 2016
开发平台(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://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1458629994.A.056.html
1F:→ fatrabitree: 载下来再判断会不会比较容易 03/22 15:06
2F:→ x000032001: 根据Content-Type判断是否为application/octet-stream 03/22 16:52
3F:→ x000032001: 这样不知道是否可行 03/22 16:52