【发布时间】:2011-05-08 05:37:09
【问题描述】:
我正在尝试从 xml 文件发送 SOAP 请求并发送到 SOAP 服务,然后使用 libcurl 在将响应保存到文件时读取响应。
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,所以我确定我做错了很多事情。我真的一直在拼凑几个不同的教程来让它做我想做的事情。任何帮助将不胜感激。
【问题讨论】:
-
如果您尝试不同的 URL 会发生什么?
-
嗯,这就是 Web 服务的 url。所以更改地址并没有多大帮助。
-
尝试设置
CURLOPT_VERBOSE选项,这样您就可以看到发生了什么。
标签: c http soap xmlhttprequest libcurl