我有通过 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;
}
这基本上连接到了服务器,我得到了 OKcurl_easy_perform()
。我可以在 Wireshark 上看到连接。
两个问题:
1) 这是通过 POST 发送数据的正确方法吗?
2)如何得到申请的回复。
我想我需要使用curl_easy_recv()
但我不明白怎么做。欢迎任何示例代码。
谢谢。