2016-11-21 9 views
0

웹 서비스의 푸시 메시지를 무한정 들어야합니다. 콘텐츠에 업데이트가있을 때마다 SOAP 응답 메시지를 보냅니다. 메시지를받은 후에는 구문을 분석하여 구조체에 저장해야합니다. 다음은 내 코드입니다.푸시 메시지를 듣습니까?

CURL *curl; 
CURLcode res; 
const char *onlineWebServiceRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soap:Body> <GetCitiesByCountry xmlns=\"http://www.webserviceX.NET\"> <CountryName>Netherlands</CountryName>  </GetCitiesByCountry> </soap:Body> </soap:Envelope>"; 

if(curl){ 

    curl_easy_setopt(curl, CURLOPT_URL, "http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry"); 

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, onlineWebServiceRequest); 

    res = curl_easy_perform(curl);  
    // call some other method to parse the res 
    curl_easy_cleanup(curl);  
} 

의심 : 그것은 푸시 메시지를받을 수있는 올바른 방법인가? 그렇다면,

위 코드는 웹 서비스와의 연결 상태를 확인하지 않습니다. 어떻게 확인할 수 있습니까?

만약 아니라면, 내가 오픈 소스에서 가지고있는 다른 옵션은 무엇입니까?

미리 감사드립니다.

답변

1

오류를 확인하는 것이 좋습니다.

이렇게하려면 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; 
} 
+0

old_mountain 안녕하세요, 감사합니다. 위의 코드에서 나는 res가 웹 서비스에 연결되어 있는지 여부 만 반환하는지 이해했습니다. 그러나 웹 서비스에서 실제 응답을받는 위치는 어디입니까? – Kid

+0

@Kid 내 편집을 참조하십시오. 네가 한 말을 이해했는지 모르겠다. 서버가 오류로 응답하면 'errbuf'에 오류가 포함되어야합니다. 'res'는 모든 것이 잘되었는지 아닌지를 확인하기위한 것입니다. –

+0

버퍼에는 오류 메시지 만 저장됩니다. – Kid