Jump to content

Recommended Posts

Posted (edited)

Hello

First i want to say that most of my problems i can solve while scripting by looking at the forum or in the help file but im stuck on this specific problem. I have been writing scripts to consume Web services and this works fine when its just plain XML responses. I work on a project now where i need to consume a SOAP response with an Attachment. I can make the plain SOAP call and get a response as well but i dont know how to handle the multipart and binary data. I have tried ADODB.Stream to write the data to a file but this just writes everything. I would be very grateful if someone could point me into the right direction.

Please find below the Request and the Response i get at the momen

 

$objHTTP = ObjCreate("Microsoft.XMLHTTP")
$objReturn = ObjCreate("Msxml2.DOMDocument.3.0")
$oDATA=ObjCreate("ADODB.Stream")

$strEnvelope = '<?xml version="1.0" encoding="UTF-8"?>' _
'<soapenv:Envelope' _
'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' _
'xmlns:xsd="http://www.w3.org/2001/XMLSchema"' _
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' _
'<soapenv:Body>' _
'<ns1:getStorageAccessTicket' _
'soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"' _
'xmlns:ns1="NBRStorageService">' _
'<siteId xsi:type="xsd:long">123456</siteId>' _
'<username xsi:type="xsd:string">USERNAME</username>' _
'<password xsi:type="xsd:string">PASSWORD</password>' _
'</ns1:getStorageAccessTicket>' _
'</soapenv:Body>'
'</soapenv:Envelope>' _

; Set up to post to the server
$objHTTP.open ("post", "API end point" , False)

; Set a standard SOAP/ XML header for the content-type
$objHTTP.setRequestHeader ("Content-Type", "application/xml")

; Set a header for the method to be called
$objHTTP.setRequestHeader ("SOAPAction", 'getStorageAccessTicket')

ConsoleWrite("Content of the Soap envelope :" & @CR & $strEnvelope & @CR & @CR )

; Make the SOAP call
$objHTTP.send ($strEnvelope)

; Get the return envelope
$strReturn = $objHTTP.responsetext

With $oDATA
        .Type=1
        .Open
        .Write($objHTTP.ResponseBody)
        .SaveToFile(@ScriptDir&'\captcha.arf', 2)
        .Close
    EndWith
EndFunc

RESPONSE:
'------=_Part_5_987625235.1444159091234'
'Content-Type: text/xml; charset=UTF-8'
'Content-Transfer-Encoding: binary'
'Content-Id: <DF72463F6FAFE80B930059E761DF7225>'

'<?xml version="1.0" encoding="UTF-8"?>'
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
 '<soapenv:Body>'
  '<ns1:downloadNBRStorageFileResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="NBRStorageService">'
   '<ns1:downloadNBRStorageFileReturn xsi:type="soapenc:Array" soapenc:arrayType="ns1:DataHandler[2]" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">'
    '<item href="cid:FE782065C11E6558C9C8925EBE3D687F"/>'
    '<item href="cid:3CF0B37E4C5932A3E60EBA5226CFC30A"/>'
   '</ns1:downloadNBRStorageFileReturn>'
  '</ns1:downloadNBRStorageFileResponse>'
 '</soapenv:Body>'
'</soapenv:Envelope>'
------=_Part_5_987625235.1444159091234
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: <FE782065C11E6558C9C8925EBE3D687F>

Attachment.arf
2342371
false
------=_Part_5_987625235.1444159091234
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: <3CF0B37E4C5932A3E60EBA5226CFC30A>

 

Edited by Moxom160
correction
  • Moderators
Posted

Moxom160,

.SaveToFile(@ScriptDir&'\captcha.arf', 2)

As the Forum rules are clear on the fact that we do not support anything that deals with CAPTCHAs, could you please explain what this line is doing.

M23

 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

Hello Melba

I think there is missunderstanding. I was trying around different bits of code and found one snipped where the file name was set to captcha.png. since i was only testing i did not change the file name to something else. Im not planning on dealing with CAPTCHAs or anything like this.

To prove my indention please find below a link to the service im trying to access. Im trying to retrieve webex recordings via the NBR(network based recording) API.

https://developer.cisco.com/site/webex-developer/develop-test/nbr-web-services-api/api-functions.gsp#downloadNBRStorageFile

Sorry for the missunderstanding.

regards

  • Moderators
Posted

Moxom160,

Fine - but please be careful which "snippets" you use in future.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

You need to parse returned data to extract the attachment. It shouldn't be hard since it's standard MIME.

Can you post the exact response? Parts are missing from your post.

♡♡♡

.

eMyvnE

Posted

Hello trancexx

The response i posted above is every thing i get in human readable form .after the last ------=_Part_5_987625235.1444159091234 and the content ids it streams down binary data.

I have tested today to stream the reponse via ADODB.Stream to a local file and this works fine. The only think im stuck at on how to seperate the binary data from the top 30 lines of response. Im only interested in the binary which i require to generate the recording file.

I have attached a response so you can have look at it. Thank you already for looking at this.

test.xml

Posted

Does this help further? I have this from the API doc from Cisco.

Soap Response:

  HTTP/1.1 200 OK
  Date: Wed, 28 Apr 2010 10:36:46 GMT
  Content-Type: multipart/related; type="text/xml";
  start="<92AE6C82DBBD12B997D03C502A058511>
    "; boundary="----=_Part_0_23877553.1272451006846"
    Connection: Close
    ------=_Part_0_23877553.1272451006846
    Content-Type: text/xml; charset=UTF-8
    Content-Transfer-Encoding: binary
    Content-Id: <92AE6C82DBBD12B997D03C502A058511>
  <?xml version="1.0" encoding="utf-8"?>
  <soapenv:Envelope
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
      <ns1:downloadNBRStorageFileResponse
  soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:ns1="NBRStorageService">
        <downloadNBRStorageFileReturn
  soapenc:arrayType="ns1:DataHandler[2]" xsi:type="soapenc:Array"
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
          <downloadNBRStorageFileReturn
  href="cid:B0A8EBE4A6E3C12D01C9AA96201968E5"
  xsi:type="ns1:DataHandler"/>
            <downloadNBRStorageFileReturn
  href="cid:6E5E9F315D7E46BF51A3729E71BD10D3"
  xsi:type="ns1:DataHandler"/>
          </downloadNBRStorageFileReturn>
        </ns1:downloadNBRStorageFileResponse>
      </soapenv:Body>
    </soapenv:Envelope>
  ------=_Part_0_23877553.1272451006846
  Content-Type: application/octet-stream
  Content-Transfer-Encoding: binary
  Content-Id: <B0A8EBE4A6E3C12D01C9AA96201968E5>
    a.arf
    188607
    ------=_Part_0_23877553.1272451006846
    Content-Type: application/octet-stream
    Content-Transfer-Encoding: binary
    Content-Id: <6E5E9F315D7E46BF51A3729E71BD10D3>
  .............
    
Posted

Does this work?

$sFile = "test.xml"
$sCID = "16F8B262D5B9C561D8899E638D3B3D8C"
$sOutFile = "YourAttachment.txt"

GetCIDAttachment($sFile, $sCID, $sOutFile)

If @error Then
    $sErrDesc = "You can't use this method because "
    Switch @error
        Case 1
            $sErrDesc &= "it sucks for you."
        Case 2
            $sErrDesc &= "getting stream failed."
        Case 3
            $sErrDesc &= "loading EML failed."
        Case 4
            $sErrDesc = "Saving to file failed."
        Case 5
            $sErrDesc = "No such attachment exists."
    EndSwitch
    MsgBox(4096, "Error", $sErrDesc & @CRLF)
Else
    MsgBox(4096, "Yay!", "Attachment with Content-Id <" & $sCID & '> extracted to "' & $sOutFile & '"' & @CRLF)
EndIf





Func GetCIDAttachment($sPath, $sCID, $sOutFile = Null)
    Local $oErrHandler = ObjEvent("AutoIt.Error", GetAttachmentSize) ; Red Jon fucked up COM

    Local $oMessage = ObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}")
    If @error Then Return SetError(1, 0, 0)

    Local $oMessageStream = $oMessage.GetStream()
    If @error Then Return SetError(2, 0, 0)

    $oMessageStream.LoadFromFile($sPath)
    If @error Then Return SetError(3, 0, 0)

    $oMessageStream.Flush() ; this is essential

    For $oBodyPart In $oMessage.BodyPart.BodyParts
        For $oField In $oBodyPart.Fields
            If $oField.Name = "urn:schemas:mailheader:content-id" And $oField.Value = "<" & $sCID & ">" Then
                If $sOutFile Then Return $oBodyPart.GetDecodedContentStream().SaveToFile($sOutFile, 2) ; adSaveCreateOverWrite
                If @error Then Return SetError(4, 0, 0)
                Return $oBodyPart.GetDecodedContentStream().Read()
            EndIf
        Next
    Next

    Return SetError(5, 0, 0)
EndFunc

It should extract "attachment" with Content-Id <16F8B262D5B9C561D8899E638D3B3D8C> from "test.xml" to file named "YourAttachment.txt". All located in the script's folder.

♡♡♡

.

eMyvnE

Posted (edited)

Hello trancexx

thanks for the script but running the above on my XML gives me "no such attachment exists".  I have tried different Content-ID's to see if i get different result but its always the same error.

 

 

Edited by Moxom160
Posted
  On 10/8/2015 at 6:22 AM, Moxom160 said:

Hello trancexx

thanks for the script but running the above on my XML gives me "no such attachment exists".  I have tried different Content-ID's to see if i get different result but its always the same error.

 

 

I told you that the file you posted here isn't full. It's missing header part where boundary is defined.

♡♡♡

.

eMyvnE

Posted

My guess is that you're missing headers. For your original code it could be:

With $oDATA
    .Type = 2
    .Open()
    .WriteText($objHTTP.getAllResponseHeaders() & $objHTTP.ResponseText)
    .SaveToFile(@ScriptDir & '\captcha.arf', 2)
    .Close()
EndWith

 

♡♡♡

.

eMyvnE

Posted

Hello trancexx

I have just tried you suggestions and you were absolutly right i need to get the full headers and formating corrected. Its working now perfectly. thank you for the support.

Have day.

mox

Posted

@trancexx

As always, again I learned something interesting, as I wast thought that IMessage is only for EML , now I re read MSDN doc for this.
Thanks
 

mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...