Jump to content

CreateMHTMLBody with cookies


Recommended Posts

Hi, I'm trying to save some pages as MHT and I found this function here on the forum.

Func _INetGetMHT( $url, $file )
    Local $msg = ObjCreate("CDO.Message")
    If @error Then Return False
    Local $ado = ObjCreate("ADODB.Stream")
    If @error Then Return False

    With $ado
        .Type = 2
        .Charset = "US-ASCII"
        .Open
        
    EndWith
    
    $msg.CreateMHTMLBody($url, 0)
    $msg.DataSource.SaveToObject($ado, "_Stream")
    FileDelete($file)
    $ado.SaveToFile($file, 1)
    $msg = ""
    $ado = ""
    Return True
EndFunc

The problem is, I need to add cookies to the http request to be able to access the page I'm trying to save.

By looking at the MSDN page for CreateMHTMLBody, I can see that there is a httpcookies field but I have no

idea how to implement it in AutoIt.

Link to comment
Share on other sites

LOL

; COM error handler
Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
Func _ErrFunc()
    ConsoleWrite("COM Error, ScriptLine(" & $oError.scriptline & ") : Number 0x" & Hex($oError.number, 8) & " - " & $oError.windescription & @CRLF)
EndFunc   ;==>_ErrFunc




Global $sURL = "http://dikydee.dyndns.org/test.php" ; whatever here
Global $sCookieString = "TestCookie=ValueForTheCookie"

ConsoleWrite(_MHT($sURL, $sCookieString) & @CRLF)

; Or maybe to save to file
;~ FileWrite($sYourFileHere, _MHT($sURL, $sCookieString))


Func _MHT($sURL, $sCookie = "")
    Local $oCDO = ObjCreate("CDO.Message")
    If @error Then Return SetError(1, 0, "") ; Couldn't create CDO.Message object

    If $sCookie Then
        Local $oConfig = $oCDO.Configuration
        If IsObj($oConfig) Then
            Local $oFields = $oConfig.Fields
            If IsObj($oFields) Then
                $oFields.Item("http://schemas.microsoft.com/cdo/configuration/httpcookies") = $sCookie
                $oFields.Update
            EndIf
        EndIf
    EndIf

    $oCDO.CreateMHTMLBody($sURL)
    If $oError.Number Then Return SetError(2, 0, "") ; Check sURL you're passing. Is it ok? Omit this line if there is no COM error handler.

    Local $oStream = $oCDO.GetStream
    If IsObj($oStream) Then Return $oStream.ReadText

    Return SetError(3, 0, "") ; GetStream method failed
EndFunc   ;==>_MHT

♡♡♡

.

eMyvnE

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...