2017-09-24 72 views
0

我已經被填充在C++如何發佈載體的HTTP端點

std::vector<uint8_t> bytes; 

使用libcurl的以下數據類型,我怎麼張貼此HTTP端點?

嘗試下面的代碼,但我認爲它不會與我的職務數據

CURL *curl; 
CURLcode res; 

curl_global_init(CURL_GLOBAL_ALL); 
curl = curl_easy_init(); 
if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, "http://1.2.3.4:9002/multicastdataclient-message"); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &bytes); 
    res = curl_easy_perform(curl); 
    if(res != CURLE_OK) 
    fprintf(stderr, "curl_easy_perform() failed: %s\n", 
      curl_easy_strerror(res)); 
    curl_easy_cleanup(curl); 
} 
+0

你試過哪些捲曲樣本? –

+0

@ArtemyVysotsky在我的文章中更新了示例代碼!感謝回覆 ! – KarthikJ

+1

'bytes.data()'或'&bytes [0]'可能是你正在尋找的而不是'&bytes'。看起來您還需要設置數據的大小,並根據您可能需要對其進行編碼的內容進行設置。 https://curl.haxx.se/libcurl/c/CURLOPT_POSTFIELDS.html –

回答

0

基於所有意見的工作,下面的代碼工作!

CURL * curl; CURLcode res;

curl_global_init(CURL_GLOBAL_ALL); 
curl = curl_easy_init(); 
if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, "http://1.2.3.4:9002/message"); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, bytes.size()); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, bytes.data()); 

    res = curl_easy_perform(curl); 
    if(res != CURLE_OK) 
    fprintf(stderr, "curl_easy_perform() failed: %s\n", 
    curl_easy_strerror(res)); 
    curl_easy_cleanup(curl); 
} 
else 
{ 
    DBG("curl empty !!"); 
}