2010-12-14 13 views
2

libcurlを使用してcプログラムからxmlデータをWebサイトにPOSTしようとしています。私は、Linuxではこのようなカールをコマンドラインプログラムを使用する場合には、正常に動作します:c libcurl POSTが一貫して機能しない

カール-X POST -H 'コンテンツタイプ:text/xmlでの' '私のXMLデータ' http://test.com/test.php

を-d(I

しかし、私はlibcurlを使って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

関連する問題