libcurl 404检测 我在我的C++程序中用LIbCURL进行文件下载。如何检测请求是否为404,而不执行文件写入?代码是: void GameImage::DownloadImage(string file_name) { string game_name; game_name = file_name.substr(file_name.find_last_of("/")+1); CURL *curl; FILE *fp; CURLcode res; string url = "http://site/"+game_name+".png"; string outfilename = file_name+".png"; cout<<"INFO; attempting to download "<<url<<"..."<<endl; curl = curl_easy_init(); if (curl) { cout<<"INFO; downloading "<<url<<"..."<<endl; fp = fopen(outfilename.c_str(), "wb"); cout<<"INFO; trying to open "<<outfilename<<" for file output"<<endl; if (fp != NULL) { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GameImage::WriteData); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_FAILONERROR, true); res = curl_easy_perform(curl); long http_code = 0; curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); curl_easy_cleanup(curl); fclose(fp); } else { cout<<"GameImage::DownloadImage; Couldn't open output file"<<endl; } } } size_t GameImage::WriteData(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written; written = fwrite(ptr, size, nmemb, stream); return written; } void GameImage::DownloadImage(字符串文件名){ 字符串游戏名称; game\u name=file\u name.substr(file\u name.find\u last\u of(“/”)+1); 卷曲*卷曲; 文件*fp; 卷曲编码; 字符串url=”http://site/“+游戏名称+”.png”; 字符串outfilename=文件名+“.png”; cout
您可以检查libcurl 404检测 我在我的C++程序中用LIbCURL进行文件下载。如何检测请求是否为404,而不执行文件写入?代码是: void GameImage::DownloadImage(string file_name) { string game_name; game_name = file_name.substr(file_name.find_last_of("/")+1); CURL *curl; FILE *fp; CURLcode res; string url = "http://site/"+game_name+".png"; string outfilename = file_name+".png"; cout<<"INFO; attempting to download "<<url<<"..."<<endl; curl = curl_easy_init(); if (curl) { cout<<"INFO; downloading "<<url<<"..."<<endl; fp = fopen(outfilename.c_str(), "wb"); cout<<"INFO; trying to open "<<outfilename<<" for file output"<<endl; if (fp != NULL) { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GameImage::WriteData); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_FAILONERROR, true); res = curl_easy_perform(curl); long http_code = 0; curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); curl_easy_cleanup(curl); fclose(fp); } else { cout<<"GameImage::DownloadImage; Couldn't open output file"<<endl; } } } size_t GameImage::WriteData(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written; written = fwrite(ptr, size, nmemb, stream); return written; } void GameImage::DownloadImage(字符串文件名){ 字符串游戏名称; game\u name=file\u name.substr(file\u name.find\u last\u of(“/”)+1); 卷曲*卷曲; 文件*fp; 卷曲编码; 字符串url=”http://site/“+游戏名称+”.png”; 字符串outfilename=文件名+“.png”; cout,c++,C++,您可以检查CURLE\u HTTP\u返回的\u错误 如果CURLOPT\u FAILONERROR设置为true,HTTP服务器返回的错误代码为=400。您无法获取特定的HTTP响应代码,但应该足以完成所需的操作。我知道这是一篇旧文章,但您所做的错误是没有检查retcurl_easy_perform的urn值。设置CURLOPT_FAILONERROR不会使程序崩溃,相反,它会通过名为res的返回变量通知您错误。要清除空文件,可以执行以下操作: void GameImage::Download
CURLE\u HTTP\u返回的\u错误
如果
CURLOPT\u FAILONERROR
设置为true
,HTTP服务器返回的错误代码为=400
。您无法获取特定的HTTP响应代码,但应该足以完成所需的操作。我知道这是一篇旧文章,但您所做的错误是没有检查retcurl_easy_perform的urn值。设置CURLOPT_FAILONERROR不会使程序崩溃,相反,它会通过名为res的返回变量通知您错误。要清除空文件,可以执行以下操作:
void GameImage::DownloadImage(string file_name) {
string game_name;
game_name = file_name.substr(file_name.find_last_of("/")+1);
CURL *curl;
FILE *fp;
CURLcode res;
string url = "http://site/"+game_name+".png";
string outfilename = file_name+".png";
cout<<"INFO; attempting to download "<<url<<"..."<<endl;
curl = curl_easy_init();
if (curl) {
cout<<"INFO; downloading "<<url<<"..."<<endl;
fp = fopen(outfilename.c_str(), "wb");
cout<<"INFO; trying to open "<<outfilename<<" for file output"<<endl;
if (fp == NULL) {
cout<<"GameImage::DownloadImage; Couldn't open output file"<<endl;
curl_easy_cleanup(curl);
return;
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GameImage::WriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
res = curl_easy_perform(curl);
fclose(fp);
if (res != CURLE_OK) {
cout<<"GameImage::DownloadImage; Failed to download file"<<endl;
remove(outfilename.c_str());
}
curl_easy_cleanup(curl);
}
}
size_t GameImage::WriteData(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
void GameImage::DownloadImage(字符串文件名){
字符串游戏名称;
game\u name=file\u name.substr(file\u name.find\u last\u of(“/”)+1);
卷曲*卷曲;
文件*fp;
卷曲编码;
字符串url=”http://site/“+游戏名称+”.png”;
字符串outfilename=文件名+“.png”;
我仍然可以得到一个文件,但它现在是空的。我添加了curl\u easy\u setopt(curl,CURLOPT\u FAILONERROR,true);我不在乎是否发生了错误,我只是不希望文件被留下。我想这是因为创建文件句柄的初始fp=fopen(outfilename.c_str(),“wb”);它创建了文件句柄。