【问题标题】:SOAP Request and Response Read from and to file using libcurl - CSOAP 请求和响应使用 libcurl 读取和读取文件 - C
【发布时间】: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


【解决方案1】:

好的,经过大量的摆弄和拼凑来自不同示例的代码,我已经弄清楚了如何执行上述任务。见以下代码:

#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】:

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

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

    【讨论】:

    • @matthew:你能指导我一些使用 libcurl 创建 SOAP 请求的教程,而不是 xml 文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多