2012-07-22 44 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