내 C++ 프로그램에서 libcurl을 사용하여 파일을 다운로드하고 있습니다. 요청이 404이고 파일을 쓰지 않는 경우 어떻게 감지 할 수 있습니까? 코드는 다음과 같습니다 전송이 발생한 후 내가 404 응답을 삭제할 수 있지만,이 경우에도 응답을 저장하지 좋은 것libcurl 404 detection
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;
}
.
파일은 여전히 있지만 비어 있습니다. curl_easy_setopt (curl, CURLOPT_FAILONERROR, true)를 추가했습니다. 나는 오류가 발생해도 상관 없다. 나는 파일을 남기고 싶지 않다. – zombor
질문을 지금 코드대로 업데이트 할 수 있습니까? – Alex
완료. 나는 그것이 초기 fp = fopen (outfilename.c_str(), "wb") 때문이라고 생각한다. 파일 핸들을 생성합니다. – zombor