2010-12-14 105 views
2

我正在嘗試使用libcurl將來自c程序的xml數據發佈到網站。當我在Linux中使用命令行程序,像這樣的捲曲正常工作:c libcurl POST不能正常工作

捲曲-X POST -H「內容類型:文本/ xml的」 -D「我的XML數據」 http://test.com/test.php

(我改變了實際數據的安全性)

但是,只要我嘗試使用libcurl編寫c代碼,它幾乎每次都會失敗,但每次都會失敗。這是我的c代碼:我有這個代碼在一個循環運行大約每10秒,它只會成功約每4或5次調用。我從服務器回來說「XML Head not found」的錯誤。

我試圖與指定的HTTP標頭:

struct curl_slist *chunk = NULL 
chunk = curl_slist_append(chunk, "Content-type: text/xml"); 
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); 

但我沒有任何運氣。有任何想法嗎?

回答

8

試試這個:

CURL *curl = curl_easy_init(); 
if(curl) 
{ 
    curl_easy_setopt(curl, CURLOPT_URL, "http://test.com/test.php"); 
    curl_easy_setopt(curl, CURLOPT_POST, 1); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, xmlString.c_str()); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, xmlString.length()); 
    struct curl_slist *slist = curl_slist_append(NULL, "Content-Type: text/xml; charset=utf-8"); // or whatever charset your XML is really using... 
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); 
    curl_easy_perform(curl); 
    curl_slist_free_all(slist); 
    curl_easy_cleanup(curl); 
} 
+0

這工作。謝謝。 – rplankenhorn 2010-12-14 20:09:22