无法使用C ++中的curl从Web服务接收响应 [英] Cant able to receive response from Web service using curl from C++

查看:234
本文介绍了无法使用C ++中的curl从Web服务接收响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从web服务使用curl和c ++的响应。我试着从终端调用Web服务。它工作正常,我可以收到响应。



但我无法通过c ++重现相同的内容。

  Request.xml 
< soapenv:Envelope xmlns:soapenv =http://schemas.xmlsoap.org / soap / envelope /xmlns:blz =http://thomas-bayer.com/blz/>
< soapenv:Header />
< soapenv:Body>
< blz:getBank>
< blz:blz> 50070010< / blz:blz>
< / blz:getBank>
< / soapenv:Body>
< / soapenv:Envelope>


void dataFromWebService :: connectWithWebservice(){

struct curl_slist * header = NULL;
header = curl_slist_append (header,Content-Type:text / xml);
header = curl_slist_append(header,charset = UTF-8);
header = curl_slist_append(header,SOAPAction:urn:getBank );

curl = curl_easy_init();

if(curl == NULL){

cout<<CURL is NULL;
}

const char * myUrl =http://www.thomas-bayer.com/axis2/services/BLZService;

// const char * myUrl =http://11.22.33.231:9080/VehicleInfoQueryService.asm;
if(curl){

string Mydata;

char errbuf [CURL_ERROR_SIZE];
struct MemoryStruct chunk;

chunk.memory =(char *)malloc(1); / *将根据需要由realloc上面增长* /
chunk.size = 0; / *此时没有数据* /


FILE * rfp = fopen(request.xml,r);

curl_easy_setopt(curl,CURLOPT_URL,myUrl);

// curl_easy_setopt(curl,CURLOPT_HTTPGET,1L);
curl_easy_setopt(curl,CURLOPT_READDATA,rfp);
curl_easy_setopt(curl,CURLOPT_HTTPHEADER,header);

curl_easy_setopt(curl,CURLOPT_HTTPGET,1L);
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);

curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);

char * response = NULL;
/ *将指针传递给响应作为回调参数* /
curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)& chunk);



errbuf [0] = 0;
cout<< 来自webservice的结果<< endl;

/ *执行请求,res将获得返回码* /

curlCode = curl_easy_perform(curl);

/ *将所有数据发送到此函数* /

curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);

curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)& chunk);


cout<<curl code--<<< curlCode<< endl;


//总是清除
if(curlCode!= CURLE_OK | curlCode!= 0){
size_t len = strlen(errbuf);
fprintf(stderr,\\\
libcurl:(%d),curlCode);
if(len)
fprintf(stderr,%s%s,errbuf,
((errbuf [len - 1]!='\\\
')?\\\
:));
else
fprintf(stderr,%s\\\
,curl_easy_strerror(curlCode));
} else {
/ *
*现在,我们的chunk.memory指向一个内存块chunk.size
* bytes big并且包含远程文件。
* /

printf(%lu bytes retrieved \\\
,(long)chunk.size);

cout<<My Chunck - << chunk.memory<< endl;
}
curl_easy_cleanup(curl);

}

}

 来自webservice的结果
*尝试80.152.243.114 ...
*连接到www。 thomas-bayer.com(80.152.243.114)port 80(#0)
> GET / axis2 / services / BLZService HTTP / 1.1
Host:www.thomas-bayer.com
Accept:* / *
Content-Type:text / xml
SOAPAction:urn :getBank

< HTTP / 1.1 500内部服务器错误
<服务器:Apache-Coyote / 1.1
< Content-Type:application / xml; charset = UTF-8
<日期:Fri,02 Dec 2016 10:56:36 GMT
<连接:close
< Content-Length:2388
<
*关闭连接0
curl code-- 0
检索2388字节
我的Chunck - <?xml version =1.0?>< Exception& apache.axis2.AxisFault:未找到的操作的端点引用(EPR)是/ axis2 / services / BLZService和WSA Action = null
at org.apache.axis2.engine.DispatchPhase.checkPostConditions(DispatchPhase.java :86)
at org.apache.axis2.engine.Phase.invoke(Phase.java:308)
at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:212)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:132)
at org.apache.axis2.transport.http.util.RESTUtil.invokeAxisEngine(RESTUtil.java:125)
at org.apache.axis2.transport.http.util.RESTUtil.processURLRequest(RESTUtil.java:119)
at org.apache.axis2.transport.http.AxisServlet $ RestRequestProcessor.processURLRequest(AxisServlet.java :799)
at org.apache.axis2.transport.http.AxisServlet.doGet(AxisServlet.java:242)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
在org。 apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
在org.apache.catalina。 core.StandardContextValve.invoke(StandardContextValve.java:122)
在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
在org.apache.catalina.valves.AccessLogValve。 invoke(AccessLogValve.java:950)
在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve。 java:98)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:313)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)
在java.lang.Thread .run(Thread.java:745)
< / Exception>
你好,世界!

从终端:



/p>

  curl --headerContent-Type:text / xml; charset = UTF-8--headerSOAPAction: getBank--data @ request.xml http://www.thomas-bayer.com/axis2/services/BLZService 

输出:

 <?xml version ='1.0'encoding ='UTF-8'?& 
< soapenv:Envelope xmlns:soapenv =http://schemas.xmlsoap.org/soap/envelope/>
< soapenv:Body>
< ns1:getBankResponse xmlns:ns1 =http://thomas-bayer.com/blz/>
< ns1:details>
< ns1:bezeichnung>
Deutsche Bank Filiale
< / ns1:bezeichnung>
< ns1:bic>
DEUTDEFFXXX
< / ns1:bic>
< ns1:ort>
法兰克福
< / ns1:ort>
< ns1:plz>
60254
< / nns:plz>
< / ns1:details>
< / ns1:getBankResponse>
< / soapenv:Body>
< / soapenv:Envelope>


解决方案

回答我自己的问题有点尴尬。 / p>

我必须使用Http Post与请求的char *,而不是请求的文件指针。



所以下面的工作。

  char * req =&soap_Left:Envelope xmlns:soapenv = \http://schemas.xmlsoap.org / soap / envelope / \xmlns:blz = \http://thomas-bayer.com/blz/\>< soapenv:Header />< soapenv:Body>< blz: getBank>< / soapenv:Body>< / soapenv:Envelope>;< / soapZ: 

curl_easy_setopt(curl,CURLOPT_HTTPPOST,1L);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,req);

这可能是原因 SOAP仅适用于POST方法,即使它通过HTTP传输,因为标准SOAP服务仅使用HTTP POST,因为它们需要不能包括的复杂SOAP请求(XML)在查询字符串中。



但我不知道这个例子来自curl 的作品。



谢谢。


I am trying to get response from web service using curl and c++. I have tried calling the web service from terminal. It works fine I can receive the response.

But I cannot able to reproduce the same through c++.

 Request.xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:blz="http://thomas-bayer.com/blz/">
 <soapenv:Header/>
  <soapenv:Body>
   <blz:getBank>
      <blz:blz>50070010</blz:blz>
    </blz:getBank>
  </soapenv:Body>
 </soapenv:Envelope>


void dataFromWebService :: connectWithWebservice(){    

struct curl_slist *header = NULL;
header = curl_slist_append (header, "Content-Type:text/xml");
header = curl_slist_append (header, "charset=UTF-8");
header = curl_slist_append (header, "SOAPAction:urn:getBank");

curl = curl_easy_init();    

if(curl == NULL){

    cout<<"CURL is NULL";
}

const char *myUrl = "http://www.thomas-bayer.com/axis2/services/BLZService";

//const char *myUrl = "http://11.22.33.231:9080/VehicleInfoQueryService.asm";
if(curl){

    string Mydata;

    char errbuf[CURL_ERROR_SIZE];
    struct MemoryStruct chunk;

    chunk.memory = (char*)malloc(1);  /* will be grown as needed by the realloc above */
    chunk.size = 0;    /* no data at this point */


    FILE * rfp = fopen("request.xml", "r");

    curl_easy_setopt(curl, CURLOPT_URL, myUrl);

    //curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
    curl_easy_setopt(curl, CURLOPT_READDATA, rfp);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);

    curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
    curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

     char *response = NULL;
    /* passing the pointer to the response as the callback parameter */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);



    errbuf[0] = 0;
    cout<< "The result from webservice "<<endl;

    /* Perform the request, res will get the return code */

    curlCode = curl_easy_perform(curl);

    /* send all data to this function  */

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

    curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&chunk);


    cout <<"curl code-- "<<curlCode<<endl;


    //  always cleanup
    if(curlCode != CURLE_OK  | curlCode != 0) {
        size_t len = strlen(errbuf);
        fprintf(stderr, "\nlibcurl: (%d) ", curlCode);
        if(len)
            fprintf(stderr, "%s%s", errbuf,
                    ((errbuf[len - 1] != '\n') ? "\n" : ""));
        else
            fprintf(stderr, "%s\n", curl_easy_strerror(curlCode));
    } else {
        /*
         * Now, our chunk.memory points to a memory block that is chunk.size
         * bytes big and contains the remote file.
         */

        printf("%lu bytes retrieved\n", (long)chunk.size);

        cout<<"My Chunck -- "<<chunk.memory<<endl;
    }
    curl_easy_cleanup(curl);

}

} 

output from code:

The result from webservice 
*   Trying 80.152.243.114...
* Connected to www.thomas-bayer.com (80.152.243.114) port 80 (#0)
> GET /axis2/services/BLZService HTTP/1.1
Host: www.thomas-bayer.com
Accept: */*
Content-Type:text/xml
SOAPAction:urn:getBank

< HTTP/1.1 500 Internal Server Error
< Server: Apache-Coyote/1.1
< Content-Type: application/xml;charset=UTF-8
< Date: Fri, 02 Dec 2016 10:56:36 GMT
< Connection: close
< Content-Length: 2388
< 
* Closing connection 0
curl code-- 0
2388 bytes retrieved
My Chunck -- <?xml version="1.0" ?><Exception>org.apache.axis2.AxisFault: The endpoint reference (EPR) for the Operation not found is /axis2/services/BLZService and the WSA Action = null
    at org.apache.axis2.engine.DispatchPhase.checkPostConditions(DispatchPhase.java:86)
    at org.apache.axis2.engine.Phase.invoke(Phase.java:308)
    at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:212)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:132)
    at org.apache.axis2.transport.http.util.RESTUtil.invokeAxisEngine(RESTUtil.java:125)
    at org.apache.axis2.transport.http.util.RESTUtil.processURLRequest(RESTUtil.java:119)
    at org.apache.axis2.transport.http.AxisServlet$RestRequestProcessor.processURLRequest(AxisServlet.java:799)
    at org.apache.axis2.transport.http.AxisServlet.doGet(AxisServlet.java:242)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
</Exception>
Hello, World!

From terminal:

command:

curl --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:urn:getBank" --data @request.xml http://www.thomas-bayer.com/axis2/services/BLZService

Output:

 <?xml version='1.0' encoding='UTF-8’?>
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>  
     <ns1:getBankResponse xmlns:ns1="http://thomas-bayer.com/blz/">
        <ns1:details>
              <ns1:bezeichnung>
                   Deutsche Bank Filiale
              </ns1:bezeichnung>
       <ns1:bic>
               DEUTDEFFXXX
       </ns1:bic>
       <ns1:ort>
               Frankfurt am Main
       </ns1:ort> 
       <ns1:plz>
               60254
       </ns1:plz>
      </ns1:details>
    </ns1:getBankResponse>
   </soapenv:Body>
 </soapenv:Envelope>

解决方案

it is little embarrassing to answer my own question.

I have to use Http Post with the request char* instead of file pointer of the request.

So the following works.

 char* req= "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:blz=\"http://thomas-bayer.com/blz/\">  <soapenv:Header/> <soapenv:Body>   <blz:getBank>  <blz:blz>50070010</blz:blz> </blz:getBank>  </soapenv:Body>  </soapenv:Envelope>";

 curl_easy_setopt(curl,CURLOPT_HTTPPOST, 1L);   
 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req);

This may be the reason SOAP only works with POST method even though it is transported by HTTP since "Standard SOAP services are using only HTTP POST because they require complex SOAP request (XML) which cannot be included in query string."

but I don’t know how this example from curl works.

Thanks.

这篇关于无法使用C ++中的curl从Web服务接收响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆