2017-09-24 67 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 !!"); 
}