我試圖從XML文件發送SOAP請求併發送到SOAP服務,然後使用libcurl將其保存到文件中時讀取響應。SOAP請求和響應使用libcurl讀取和寫入文件 - C
在一個XML文件中的要求範例如下:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:olm="http://127.0.0.1:1090/services/olmDSMN">
<soapenv:Header/>
<soapenv:Body>
<olm:getAllCommercialProducts soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</soapenv:Body>
</soapenv:Envelope>
我試圖使用,使讀寫libcurl的代碼如下:
#define URL "http://192.168.56.101:8088/olmDSMN"
size_t write_data(void *ptr, size_t size, size_t nmeb, void *stream)
{
return fwrite(ptr,size,nmeb,stream);
}
size_t read_data(void *ptr, size_t size, size_t nmeb, void *stream)
{
return fread(ptr,size,nmeb,stream);
}
int sendMessage (char * inFile, char * outFile) {
//writing to file initially
FILE * rfp = fopen(inFile, "r");
if(!rfp) {
perror("Read File Open:");
// exit(0);
}
FILE * wfp = fopen(outFile, "w+"); //File pointer to write the soap response
if(!wfp) {
perror("Write File Open:");
// exit(0);
}
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, URL);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_data); //Reads xml file to be sent via POST operationt
curl_easy_setopt(curl, CURLOPT_READDATA, rfp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //Gets data to be written to file
curl_easy_setopt(curl, CURLOPT_WRITEDATA, wfp); //Writes result to file
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return 1;
}
}
在上述時間不起作用。當函數被調用時只是等待(就像等待一個永遠不會到來的響應一樣)。我只是在學習libcurl,所以我確定我做了很多不正確的事情。我真的在拼湊一些不同的教程來讓它做我想做的事。任何援助將不勝感激。
如果您嘗試使用其他網址,會發生什麼情況? – chrisaycock 2010-11-11 01:57:34
那麼,這是網絡服務的網址。所以改變地址並沒有多大幫助。 – 2010-11-11 02:30:32
嘗試設置'CURLOPT_VERBOSE'選項,以便您可以看到發生了什麼。 – caf 2010-11-11 03:56:25