Jump to content

Problem with SOAP with Attachment


Recommended Posts

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
Link to comment
Share on other sites

  • Moderators

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>
  .............
    
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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:

Spoiler

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

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...