如何使用libcurl发送soap(xml)请求

时间:2019-07-03 12:08:34

标签: c++ xml curl gsoap

我正在尝试使用libcurl将xml格式的SOAP请求发送到SOAP服务,但是在soap服务中发布请求时却出现诸如“ 500内部服务器错误”之类的错误。

我已经尝试了几乎所有在线可用方法。

#include <stdio.h>
#include <curl/curl.h>
int main()
{
    FILE * wfp = fopen("res.xml", "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; charset=utf-8");
    header = curl_slist_append(header, "SOAPAction: http://localhost:62634/Service1.svc");
    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, "http://localhost:62634/Service1.svc");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "<?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:AddNumbers=\"http://localhost:62634/Service1.svc\"><soapenv:Header/><soapenv:Body><AddNumbers xmlns=\"http://tempuri.org/\"><number1>4</number1><number2>5</number2></AddNumbers></soapenv:Body></soapenv:Envelope>");

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        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");

        res = curl_easy_perform(curl);

        curl_easy_cleanup(curl);

        return 1;
    }
}

0 个答案:

没有答案