Как отправить запрос мыла (xml) с помощью libcurl - PullRequest
0 голосов
/ 03 июля 2019

Я пытаюсь отправить запрос SOAP в формате xml в службу SOAP с помощью libcurl, но при отправке запроса в мыльном сервисе я получаю сообщение об ошибке типа «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;
    }
}
...