SOAP Request and Response Read from and to file us

2019-04-12 19:42发布

问题:

I am trying to send a SOAP request from a xml file and send to a SOAP service and then read the response while saving it into a file, using libcurl.

An example request in an xml file is as followed:

<?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>

The libcurl code I'm trying to use to make the read and write is as follows:

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

At the moment the above does not work. When the function is invoked is just waits(as if waiting on a response that never comes). I'm just learning libcurl, so I'm sure I've done many things incorrectly. I've really been piecing together a couple different tutorials to get it do what I want it to do. Any assistance would be greatly appreciated.

回答1:

Ok, so after a whole load of fiddling and piecing together code from different examples I've figured out how to perform the above task. See code below:

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


回答2:

Presuming that your SOAP server expects a HTTP POST, you need to:

  • Set the CURLOPT_POST option;
  • Set the Content-Type header Content-Type: application/soap+xml using CURLOPT_HTTPHEADER;
  • Set the size of your XML request using the CURLOPT_POSTFIELDSIZE option.