libcurl HTTPS POST 数据发送?
我有通过 HTTP POST 请求接收数据的应用程序。我正在尝试使用 libcurl 打开对此应用程序的请求,发送数据并接收从应用程序返回的回复。这是我到目前为止的代码:
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
const int timeout = 30000;
char outputmessage[]="VGhpcyBpcyBqdXN0IGEgbWVzc2FnZSwgaXQncyBub3QgdGhlIHJlYWwgdGhpbmc=";
curl_easy_setopt(curl, CURLOPT_URL, "https://somehost.com/someapp");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.5");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, outputmessage);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(outputmessage));
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout/1000);
res = curl_easy_perform(curl);
if(CURLE_OK != res)
{
printf("Error: %s\n", strerror(res));
return 1;
}
// now what?
// cleanup when done
curl_easy_cleanup(curl);
}
return 0;
}
这基本上连接到服务器,我在 curl_easy_perform()
上得到了 OK。我可以在 Wireshark 上看到连接。
两个问题:
1) 这是通过 POST 发送数据的正确方法吗?
2)如何得到申请的回复。
我认为我需要使用 curl_easy_recv()
但我不知道如何使用。欢迎任何示例代码。 谢谢。
I have application that received data via HTTP POST requests. I'm trying to use libcurl to open a request to this app, send the data and receive the reply back from the app. This is the code I have so far:
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
const int timeout = 30000;
char outputmessage[]="VGhpcyBpcyBqdXN0IGEgbWVzc2FnZSwgaXQncyBub3QgdGhlIHJlYWwgdGhpbmc=";
curl_easy_setopt(curl, CURLOPT_URL, "https://somehost.com/someapp");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.5");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, outputmessage);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(outputmessage));
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout/1000);
res = curl_easy_perform(curl);
if(CURLE_OK != res)
{
printf("Error: %s\n", strerror(res));
return 1;
}
// now what?
// cleanup when done
curl_easy_cleanup(curl);
}
return 0;
}
This essentially connects to the server, I get a OK on the curl_easy_perform()
. I can see the connection on Wireshark.
Two questions:
1) Is this the correct way of sending data via a POST, and
2) How do I get the reply from the application.
I thought I need to use curl_easy_recv()
but I can't see how. Any sample code is welcome.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您还应该考虑为响应标头添加一个处理程序:
如果这是一个设计供应用程序使用的 Web 服务,则其回复可能是仅标头回复,没有响应数据。标头将包含以下数据:
您要查找的主要内容是表示请求正常的
200
代码。处理来自标头或响应的数据的函数应采用 void*,如果将其用作字符串,则需要以空值终止它。有关信息,这是我处理传入数据的方式(在本例中,我仅在应用程序中使用返回代码 - 响应正文仅转到日志文件):
为了简洁起见,删除了错误处理!
I think you should also consider adding a handler for the response headers:
It may be that if this is a web service designed for use by an application, its reply is a header-only reply, with no response data. The header would contain data such as:
Where the main thing you're looking for is the
200
code saying the request was OK.The functions to handle the data from either header or response should take a void*, and you'll need to null-terminate it if you're using it as a string. For info this is how I handle the incoming data (in this case I only use the return code in my application - the response body just goes to a log file):
Error handling removed for brevity!
您不需要调用任何接收函数。当控制权传递给写入完成回调时,收到的响应将返回给您。设置该回调,如下所示:
curl_res =curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, RecvResponseCallback);
在调用执行之前。
像这样定义回调:
响应数据由 data 参数指向(不是 ptr 参数,这是您想要的其他内容看看你什么时候开始工作)。
退出 RecvResponseCallback 时,请务必返回
(size_t) size * nmemb
You don't need to call any receive function. The received response comes back to you when control passes to the write-finished callback. Set up that callback something like this:
curl_res = curl_easy_setopt( handle, CURLOPT_WRITEFUNCTION, RecvResponseCallback );
before calling perform.
Define the callback like this:
The response data is pointed by the data arg (not the ptr arg, which is something else that you will want to look at when you get this working).
When you exit RecvResponseCallback, be sure to return
(size_t) size * nmemb