curl调用soap

2017-02-06 11:36

最近的项目,需要研究一下libcurl提交XML请求,顺便也用curl测试了一下SOAP请求。以下给出三种案例,方便以后查询。

curl提交无需认证的SOAP请求

这个案例使用webxml.com.cn的中国股票Web Service服务,SOAP消息和执行命令如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">  
   <soapenv:Header/>  
   <soapenv:Body>  
      <web:getStockInfoByCode>  
         <!--Optional:-->  
         <web:theStockCode>sh600000</web:theStockCode>  
      </web:getStockInfoByCode>  
   </soapenv:Body>  
</soapenv:Envelope>  

调用

curl -v -H "Content-Type: text/xml;charset=UTF-8" -H "SOAPAction: \"http://WebXml.com.cn/getStockInfoByCode\"" -d @stock_request http://webservice.webxml.com.cn/WebServices/ChinaStockWebService.asmx

curl提交需要基本认证(Basic Authentication)的SOAP请求

这个案例使用公司的一个应用,以免泄密,屏蔽了End point等信息,SOAP消息和执行命令如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:msmi="http://aaa.xxx.com/yyy">  
   <soapenv:Header/>  
   <soapenv:Body>  
      <msmi:echo>  
         <msmi:EchoMessage>hello</msmi:EchoMessage>  
      </msmi:echo>  
   </soapenv:Body>  
</soapenv:Envelope>  

调用

curl -v -H "Content-Type: text/xml;charset=UTF-8" -H "Authorization: Basic VkYt5EstbVNN5T0odlR42EMZaDlBMyE=" -k -d @basic_auth_request https://aaa.xxx.com/Services/yyy 

curl提交需要用户名令牌认证(UsernameToken Authentication)的SOAP请求

同样也屏蔽了End point、密码等信息,SOAP消息和执行命令如下:

<soapenv:Envelope xmlns:echo="http://echo.ws.aaa.xxx.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">  
   <soapenv:Header>  
      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">  
         <wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">  
            <wsse:Username>username</wsse:Username>  
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>  
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">X11BrdgMh/Outv2Tii83og==</wsse:Nonce>  
            <wsu:Created>2011-01-26T09:37:26.953Z</wsu:Created>  
         </wsse:UsernameToken>  
      </wsse:Security>  
      <echo:customerId>0000</echo:customerId>  
   </soapenv:Header>  
   <soapenv:Body>  
      <echo:showVersionInformation/>  
   </soapenv:Body>  
</soapenv:Envelope>  

调用

curl -v -k -d @usernametoken_auth_request https://aaa.sp.xxx.com/aaa/4.0/services/yyy

其中,加入-v参数是为了显示HTTP头信息,至于何时需要-H参数添加相应的HTTP头信息,可以参考soapUI-3.6.1的执行结果。