首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++:如何通过curl调用使用HTTP post请求发送二进制数据(协议数据

C++:如何通过curl调用使用HTTP post请求发送二进制数据(协议数据
EN

Stack Overflow用户
提问于 2019-11-26 14:39:05
回答 2查看 1.7K关注 0票数 1

我正在尝试使用protobuf数据在url上发出POST请求。我不知道如何/在哪里添加二进制数据。下面是我的C++程序。“

代码语言:javascript
运行
复制
void sendContent()
{
    using namespace std;
    int Error = 0;
    //CString str;

    CURL* curl;
    CURLcode res;

    struct curl_slist *headerlist = NULL;
    curl_global_init(CURL_GLOBAL_ALL);

    curl = curl_easy_init();

    headerlist = curl_slist_append(headerlist, "Content-Type: application/x-protobuf");

    //Set URL to recevie POST
    curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
    curl_easy_setopt(curl, CURLOPT_POST, true);
    curl_easy_setopt(curl, CURLOPT_HEADER, true);
    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:9090/info");  
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);

    res = curl_easy_perform(curl);

    curl_easy_cleanup(curl);
    curl_global_cleanup();

}"
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-26 14:52:49

您应该通过curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);设置数据指针。同时,您应该通过curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, length_of_data);设置数据大小。

您可以在there中找到libcurl post示例。

我复制了下面的程序。

代码语言:javascript
运行
复制
#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  /* In windows, this will init the winsock stuff */ 
  curl_global_init(CURL_GLOBAL_ALL);

  /* get a curl handle */ 
  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */ 
    curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
    /* Now specify the POST data */
    /* size of the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, length_of_data);
    /* binary data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
  return 0;
}
票数 2
EN

Stack Overflow用户

发布于 2019-11-26 17:10:11

修正你最初的建议可能会是这样的(基于libcurl网站的the simplepost example ):

代码语言:javascript
运行
复制
#include <curl/curl.h>

int binarypost(char *binaryptr, long binarysize)
{
  CURL *curl;
  CURLcode res = CURLE_OK;
  struct curl_slist *headerlist = NULL;
  headerlist = curl_slist_append(headerlist, "Content-Type: application/x-protobuf");

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:9090/info");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, binaryptr);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, binarysize);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);

    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return (int)res;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59045074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档