오류를 확인하는 것이 좋습니다.
이렇게하려면 CURLOPT_ERRORBUFFER
옵션을 사용 설정해야합니다.
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
그리고 버퍼를 추가 (적어도 CURL_ERROR_SIZE
바이트 큰해야합니다).
char errbuf[CURL_ERROR_SIZE];
또한 오류에 모든 응답 코드> = 300로 변환 컬 강제로 true로 CURLOPT_FAILONERROR
을 설정할 수 있습니다.
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
는 데이터 (
CURLOPT_WRITEFUNCTION
) 및 메모리 (
CURLOPT_WRITEDATA
)의 덩어리를 작성하는 기능을 설정해야 서버에서 실제 출력을 얻을합니다.
[1] 및 [2]에서 적응 완전한 예 : 응답에 대한
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int main(void)
{
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
CURL curl = curl_easy_init();
if(curl) {
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = (char *)malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
char errbuf[CURL_ERROR_SIZE];
/* specify URL to get */
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* send all data to this function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
/* force curl to fail error when http code >= 300 */
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
/* provide a buffer to store errors in */
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
/* set the error buffer as empty before performing a request */
errbuf[0] = 0;
/* perform the request */
res = curl_easy_perform(curl);
/* if the request did not complete correctly, show the error
information. if no detailed error information was written to errbuf
show the more generic information from curl_easy_strerror instead.
*/
if(res != CURLE_OK) {
size_t len = strlen(errbuf);
fprintf(stderr, "\nlibcurl: (%d) ", res);
if(len)
fprintf(stderr, "%s%s", errbuf,
((errbuf[len - 1] != '\n') ? "\n" : ""));
else
fprintf(stderr, "%s\n", curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/
printf("%lu bytes retrieved\n", (long)chunk.size);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl);
free(chunk.memory);
}
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return 0;
}
old_mountain 안녕하세요, 감사합니다. 위의 코드에서 나는 res가 웹 서비스에 연결되어 있는지 여부 만 반환하는지 이해했습니다. 그러나 웹 서비스에서 실제 응답을받는 위치는 어디입니까? – Kid
@Kid 내 편집을 참조하십시오. 네가 한 말을 이해했는지 모르겠다. 서버가 오류로 응답하면 'errbuf'에 오류가 포함되어야합니다. 'res'는 모든 것이 잘되었는지 아닌지를 확인하기위한 것입니다. –
버퍼에는 오류 메시지 만 저장됩니다. – Kid