【发布时间】:2011-10-21 14:17:01
【问题描述】:
我的应用程序通过 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() 上得到了一个确定。我可以在 Wireshark 上看到连接。
两个问题:
1) 这是通过 POST 发送数据的正确方式吗,以及
2) 如何获得应用程序的回复。
我认为我需要使用curl_easy_recv(),但我不知道如何使用。欢迎任何示例代码。
谢谢。
【问题讨论】: