【问题标题】:How to do a SOAP wsdl web services call from the command line如何从命令行调用 SOAP wsdl Web 服务
【发布时间】:2012-08-26 16:48:24
【问题描述】:

我需要对https://sandbox.mediamind.com/Eyeblaster.MediaMind.API/V2/AuthenticationService.svc?wsdl 进行 SOAP Web 服务调用,并在传递参数时使用 ClientLogin 操作:ApplicationKey、Password 和 UserName。响应是 UserSecurityToken。它们都是字符串。

这里是完整解释我想要做什么的链接: https://sandbox.mediamind.com/Eyeblaster.MediaMind.API.Doc/?v=3

如何在命令行上执行此操作? (Windows 和/或 Linux 会有所帮助)

【问题讨论】:

    标签: soap wsdl


    【解决方案1】:

    使用 CURL:

    SOAP_USER='myusername'
    PASSWORD='mypassword'
    AUTHENTICATION="$SOAP_USER:$PASSWORD"
    URL='http://mysoapserver:8080/meeting/aws'
    SOAPFILE=getCurrentMeetingStatus.txt
    TIMEOUT=5
    

    CURL 请求:

    curl --user "${AUTHENTICATION}" --header 'Content-Type: text/xml;charset=UTF-8' --data @"${SOAPFILE}" "${URL}" --connect-timeout $TIMEOUT
    

    我用它来验证响应:

    http_code=$(curl --write-out "%{http_code}\n" --silent --user "${AUTHENTICATION}" --header 'Content-Type: text/xml;charset=UTF-8' --data @"${SOAPFILE}" "${URL}" --connect-timeout $TIMEOUT --output /dev/null)
    if [[ $http_code -gt 400 ]];  # 400 and 500 Client and Server Error codes http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    then
    echo "Error: HTTP response ($http_code) getting URL: $URL"
    echo "Please verify parameters/backend. Username: $SOAP_USER Password: $PASSWORD Press any key to continue..."
    read entervalue || continue
    fi
    

    【讨论】:

    • $USERNAME 解析为我的 linux 用户名,在我的脚本中更改为 $USER
    【解决方案2】:

    这是另一个示例 CURL - 对银行 swift codes 的 SOAP (WSDL) 请求

    请求

    curl -X POST http://www.thomas-bayer.com/axis2/services/BLZService \
      -H 'Content-Type: text/xml' \
      -H 'SOAPAction: blz:getBank' \
      -d '
      <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>10020200</blz:blz>
          </blz:getBank>
        </soapenv:Body>
      </soapenv:Envelope>'
    

    回应

    < HTTP/1.1 200 OK
    < Server: Apache-Coyote/1.1
    < Content-Type: text/xml;charset=UTF-8
    < Date: Tue, 26 Mar 2019 08:14:59 GMT
    < Content-Length: 395
    < 
    <?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>BHF-BANK</ns1:bezeichnung>
            <ns1:bic>BHFBDEFF100</ns1:bic>
            <ns1:ort>Berlin</ns1:ort>
            <ns1:plz>10117</ns1:plz>
          </ns1:details>
        </ns1:getBankResponse>
      </soapenv:Body>
    </soapenv:Envelope>
    

    【讨论】:

    • +1 表示多个标题行(另一个 -H),因为将所有标题都放在一个位置很整洁。在 SAP 环境中工作。
    【解决方案3】:

    这是一个标准的普通 SOAP Web 服务。 SSH 在这里无关。我只是用(单行)调用它:

    $ curl -X POST -H "Content-Type: text/xml" \
        -H 'SOAPAction: "http://api.eyeblaster.com/IAuthenticationService/ClientLogin"' \
        --data-binary @request.xml \
        https://sandbox.mediamind.com/Eyeblaster.MediaMind.API/V2/AuthenticationService.svc
    

    其中request.xml文件有以下内容:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://api.eyeblaster.com/">
               <soapenv:Header/>
               <soapenv:Body>
                  <api:ClientLogin>
                     <api:username>user</api:username>
                     <api:password>password</api:password>
                     <api:applicationKey>key</api:applicationKey>
                  </api:ClientLogin>
              </soapenv:Body>
    </soapenv:Envelope>
    

    我得到了这个漂亮的 500:

    <?xml version="1.0"?>
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Body>
        <s:Fault>
          <faultcode>s:Security.Authentication.UserPassIncorrect</faultcode>
          <faultstring xml:lang="en-US">The username, password or application key is incorrect.</faultstring>
        </s:Fault>
      </s:Body>
    </s:Envelope>
    

    你试过吗?

    Read more

    【讨论】:

    • +1 soapui,一个非常有用且免费的工具,用于处理基于肥皂的网络服务。比使用命令行恕我直言要好得多。
    • 您使用的是哪个版本的 curl?我的说“无法解析主机 '--data-binary',并且 https 是“不受支持的协议”。
    • 如果我完全按照您的做法进行操作,那么我总是会从 mediamind 收到一条错误消息,提示“发生了意外的内部服务器错误”。有没有什么你没有包括在我应该做的答案中(除了用真实的替换 un/pw/key)?
    • @Marina:现在我也收到“500 Internal Server Error”。然而,这是一个服务器端错误,而不是我们的 (?),请询问 WS 提供者发生了什么。几天前它还在工作。
    • 感谢您的回答。 request.xml 中缺少 /Envelope,导致服务器响应错误。一旦我补充说它工作正常。也许这就是其他人遇到错误的问题。
    【解决方案4】:

    对于寻找 PowerShell 替代方案的 Windows 用户,这里是(使用 POST)。为了便于阅读,我将其拆分为多行。

    $url = 'https://sandbox.mediamind.com/Eyeblaster.MediaMind.API/V2/AuthenticationService.svc'
    $headers = @{
        'Content-Type' = 'text/xml';
        'SOAPAction' = 'http://api.eyeblaster.com/IAuthenticationService/ClientLogin'
    }
    $envelope = @'
        <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
            <Body>
                <yourEnvelopeContentsHere/>
            </Body>
        </Envelope>
    '@     # <--- This line must not be indented
    
    Invoke-WebRequest -Uri $url -Headers $headers -Method POST -Body $envelope
    

    【讨论】:

      【解决方案5】:
      curl --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:ACTION_YOU_WANT_TO_CALL" --data @FILE_NAME URL_OF_THE_SERVICE 
      

      以上命令对我有帮助

      例子

      curl --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:urn:GetVehicleLimitedInfo" --data @request.xml http://11.22.33.231:9080/VehicleInfoQueryService.asmx 
      

      More info

      【讨论】:

        【解决方案6】:

        对于 Windows,我发现这是可行的:

        Set http = CreateObject("Microsoft.XmlHttp")
        http.open "GET", "http://www.mywebservice.com/webmethod.asmx?WSDL", FALSE
        http.send ""
        WScript.Echo http.responseText
        

        参考:CodeProject

        【讨论】:

          【解决方案7】:

          在linux命令行下,你可以简单地执行:

          curl -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction:"  -d @your_soap_request.xml -X POST https://ws.paymentech.net/PaymentechGateway
          

          【讨论】:

            【解决方案8】:

            对于 Windows:

            将以下内容另存为 MSFT.vbs:

            set SOAPClient = createobject("MSSOAP.SOAPClient")
            SOAPClient.mssoapinit "https://sandbox.mediamind.com/Eyeblaster.MediaMind.API/V2/AuthenticationService.svc?wsdl"
            WScript.Echo "MSFT = " & SOAPClient.GetQuote("MSFT")
            

            然后在命令提示符下运行:

            C:\>MSFT.vbs
            

            参考:http://blogs.msdn.com/b/bgroth/archive/2004/10/21/246155.aspx

            【讨论】:

            • 今年 2004 年的技术至少在 Window 7 上失败了。
            猜你喜欢
            • 1970-01-01
            • 2016-08-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-03
            • 1970-01-01
            相关资源
            最近更新 更多