简体   繁体   English

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

[英]C++: How can I Send binary data(protobuf data) using HTTP post request through a curl call

I am trying trying to make a POST request on a url with protobuf data.我正在尝试使用 protobuf 数据在 url 上发出 POST 请求。 I don't know how/ where to add binary data.我不知道如何/在哪里添加二进制数据。 Below is my C++ program.下面是我的 C++ 程序。 "

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();

}"

You should set the data pointer by curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);您应该通过curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);设置数据指针curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); . . In the meantime, you should set the data size by curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, length_of_data);同时,您应该通过curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, length_of_data);设置数据大小curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, length_of_data); . .

You can find the libcurl post example in there .您可以在那里找到 libcurl 帖子示例。

And I copy the program below.我复制下面的程序。

#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;
}

Fixing your original suggestion would probably make it something like this (based on the simplepost example from the libcurl web site):修复您的原始建议可能会使其成为这样(基于 libcurl 网站上的 simplepost 示例):

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM