C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据)
C++: How can I Send binary data(protobuf data) using HTTP post request through a curl call
我正在尝试在带有protobuf数据的URL上发出POST请求。我不知道如何/在哪里添加二进制数据。以下是我的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();
}"
您应该按curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
设置数据指针。同时,您应该通过curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, length_of_data);
设置数据大小。
你可以在那里找到libcurl帖子的例子。
我复制下面的程序。
#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: %sn",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
修复您的原始建议可能会使其如下所示(基于 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;
}
相关文章:
- 在 C++/C 中使用 CURL 发出带有数据文件的 GET HTTP 请求
- C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据)
- 错误:请求"数据"中的成员"大小",其非类类型为"int [5]"
- 读取 ICMP 回显请求数据
- 其他数据以及 TCP 连接请求
- 通过HTTP请求从站点检索数据(使用QT5.4的C++)
- 在浏览器中呈现数据之前,是否可以拦截http请求并修改数据(例如使用regex替换内容)?如果是,如何
- 是指向在向函数返回值/参数和类请求传输数据期间复制的内存的指针
- 如何实现消费者生产者,消费者可以请求新的数据
- 如何通过串行端口发送请求从目录中检索数据
- 请求想法 - 如何对具有多行和 3 列的 2d 数组进行排序,维护数据行
- 卷曲,来自请求的不完整数据
- 如何处理错误“vtkTrivialProducer :此数据对象不包含请求的范围”
- 请求后中的二进制数据
- cURL 返回上次请求的数据
- 如何在一个套接字上实现并行请求和响应的非阻塞客户端-服务器通信模型,而不存在数据竞争
- std::ifstream::read()读取的数据少于请求的数据,并设置失败位,原因不明显
- http 请求在 & 符号后剪切数据 &
- 访问 QNetwork请求数据,然后再发送数据
- 通过WinInet请求JSON数据会重复部分数据