2012-07-22 74 views
6

我遇到libcurl问题。我写了简单的程序,应该发布数据(填写表格),但程序不工作。 我的形式:使用libcurl发布数据

... 
<div><label for="id_person_name">Your name</label> <input type="text" id="id_person_name" name="name" /></div> 
      <div></div> 

      <div class="clear"></div> 
      <div><label for="id_comment">Comment</label><textarea name="comment" id="id_comment" rows="10" cols="60" class="txt"></textarea></div> 
... 

计划:

#include <curl/curl.h> 
#include <iostream> 

using namespace std; 

int main(){ 

CURL *curl; 
CURLcode res; 

curl = curl_easy_init(); 

if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, "http://examplesite.com"); 
    curl_easy_setopt(curl, CURLOPT_POST, 1); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=Bjarne&comment=example"); 
res = curl_easy_perform(curl); 
curl_easy_cleanup(curl); 

cout << endl; 
} 
return 0; 
} 

当然,我测试此代码:http://curl.haxx.se/libcurl/c/postit2.html,但它不工作。

任何人都可以帮助我吗?

+1

能否请你定义“不工作”,它连接了我 – 2012-07-22 20:00:19

+0

所以我会告诉你简单的例子。我使用http://curl.haxx.se/libcurl/c/http-post.html这个程序,我测试它在这个页面上:m.se.pl/nc/comments/dodaj/27/269110/我填写地址 'curl_easy_setopt(curl,CURLOPT_URL,“http://m.se.pl/nc/comments/dodaj/27/269110/");'和 'curl_easy_setopt(curl,CURLOPT_POSTFIELDS,”name = test&comment = test“ );'并编译它 - 一切正常。我在5秒后开始我的程序。程序显示我来源这个页面(当然在添加帖子之前的源代码)。 – user1518451 2012-07-22 20:16:57

+1

在您提供的网页上,您还需要包含一些额外的信息,例如security_hash,timesamp,next等(它们是隐藏字段)。您可以在网页源代码中看到它们。我想这使服务器拒绝查询导致没有网页返回 – 2012-07-23 16:01:31

回答

3

需要设置CURLOPT_URL解决这点,检查

http://curl.haxx.se/libcurl/c/http-post.html

例如

+0

我检查了这个:http://curl.haxx.se/libcurl/c/http-post.html 但它没有发送我的数据任何建议? – user1518451 2012-07-22 12:48:51

+1

看起来像你错过了curl_global_init – 2012-07-22 14:40:26

+0

我没有错过curl_global_init 。我使用的例子(http://curl.haxx.se/libcurl/c/http-post.html)包含它。任何其他建议? – user1518451 2012-07-22 19:57:00