Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用libcurl-C从文件和向文件读取SOAP请求和响应_C_Http_Soap_Xmlhttprequest_Libcurl - Fatal编程技术网

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

使用libcurl-C从文件和向文件读取SOAP请求和响应,c,http,soap,xmlhttprequest,libcurl,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" xm

我试图从一个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,所以我肯定我做了很多错误的事情。我一直在拼凑一些不同的教程,让它做我想做的事情。非常感谢您的帮助。

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

  • 设置
    CURLOPT_POST
    选项
  • 使用
    CURLOPT_HTTPHEADER
    设置内容类型标题
    内容类型:应用程序/soap+xml
  • 使用
    CURLOPT\u POSTFIELDSIZE
    选项设置XML请求的大小

好的,在对来自不同示例的代码进行了大量的修改和拼凑之后,我已经找到了执行上述任务的方法。见下面的代码:

#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;
    }
}
#包括
#包括
#包括
#定义URL“http://192.168.56.101:8088/olmDSMN"
大小写入数据(void*ptr、大小、大小nmeb、void*stream)
{
返回fwrite(ptr、大小、nmeb、流);
}
大小读取数据(void*ptr、大小、大小nmeb、void*stream)
{
返回fread(ptr、大小、nmeb、流);
}
int sendMessage(字符*填充,字符*输出文件){
//最初写入文件
文件*rfp=fopen(填入“r”);
如果(!rfp){
perror(“读取文件打开:”);
//出口(0);
}
FILE*wfp=fopen(outFile,“w+”);//用于写入soap响应的文件指针
如果(!wfp){
perror(“写入文件打开:”);
//出口(0);
}
struct curl_slist*header=NULL;
header=curl\u slist\u append(header,“内容类型:text/xml”);
header=curl\u slist\u append(header,“SOAPAction:myur1”);
header=curl\u slist\u append(header,“传输编码:分块”);
header=curl\u slist\u append(header,“Expect:”);
卷曲*卷曲;
卷曲编码;
curl=curl_easy_init();
if(curl){
curl_easy_setopt(curl,CURLOPT_URL,URL);
卷曲容易设置(卷曲,卷曲杆,1L);
curl_easy_setopt(curl,CURLOPT_READFUNCTION,read_data);//读取通过POST操作发送的xml文件
curl\u easy\u setopt(curl,CURLOPT\u READDATA,rfp);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);//获取要写入文件的数据
curl_easy_setopt(curl,CURLOPT_WRITEDATA,wfp);//将结果写入文件
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”);
卷曲度(卷曲度,卷曲度(1L));
res=旋度(curl)\u容易执行(curl);
旋度\轻松\清洁(旋度);
返回1;
}
}

如果尝试其他URL,会发生什么情况?这就是web服务的URL。因此,更改地址没有多大帮助。请尝试设置
CURLOPT\u VERBOSE
选项,这样您就可以看到发生了什么。@matthew:您能给我介绍一些使用libcurl创建SOAP请求的教程,而不是xml文件吗。
#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;
    }
}