2009-12-01 57 views
1

下面的代码(从现有的应用程序中提取):为什么在尝试使用http post时收到CURLE_URL_MALFORMAT?

CURL *curl = curl_easy_init(); 
_ASSERTE(curl); 

string url = "http://127.0.0.1:8000/"; 

char *data = "mode=test"; 
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); 
curl_easy_setopt(curl, CURLOPT_URL, url); 
CURLcode res = curl_easy_perform(curl); 

bool success = (res == CURLE_OK); 

curl_easy_cleanup(curl); 

res值是CURLE_URL_MALFORMAT。这个URL与curl不兼容?

+0

m_checkUrl是什么类型的?您是否使用内联字符*进行了测试?即把“http://127.0.0.1:8000/”放在第三个参数中curl_easy_setopt? – 2009-12-01 18:06:12

+0

该网址应该有一个http:前缀 – 2009-12-01 18:07:03

+0

我更新了我的代码示例 - 我使用字符串而不是char * - 哎呀! – 2009-12-01 18:21:04

回答

0

嗯,简单的错误,我需要通过char *curl_easy_setopt而不是string。为了解决这个问题,我刚刚使用.c_str()这样的:

CURL *curl = curl_easy_init(); 
_ASSERTE(curl); 

string url = "http://127.0.0.1:8000/"; 

char *data = "mode=test"; 
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); 
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); 
CURLcode res = curl_easy_perform(curl); 

bool success = (res == CURLE_OK); 

curl_easy_cleanup(curl); 
相关问题