SOAP请求和响应使用libcurl-C从文件读取和到文件读取

时间:2010-11-11 01:33:37

标签: c http soap xmlhttprequest libcurl

我正在尝试从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,所以我确信我做错了很多事情。我真的拼凑了几个不同的教程,让它做我想做的事。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

好的,所以经过一整套摆弄和拼接来自不同示例的代码后,我已经找到了如何执行上述任务。请参阅以下代码:

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

#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);
    }

    struct curl_slist *header = NULL;
    header = curl_slist_append (header, "Content-Type:text/xml");
    header = curl_slist_append (header, "SOAPAction: myur1");
    header = curl_slist_append (header, "Transfer-Encoding: chunked");
    header = curl_slist_append (header, "Expect:");
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, URL);
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        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
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)-1);
//        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_VERBOSE,1L);            
        res = curl_easy_perform(curl);

        curl_easy_cleanup(curl);

        return 1;
    }
}

答案 1 :(得分:0)

假设您的SOAP服务器需要HTTP POST,您需要:

  • 设置CURLOPT_POST选项;
  • 使用Content-Type: application/soap+xml;
  • 设置Content-Type标头CURLOPT_HTTPHEADER
  • 使用CURLOPT_POSTFIELDSIZE选项设置XML请求的大小。