Jump to content
Sign in to follow this  

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"
    $msg.CreateMHTMLBody($url, 0)
    $msg.DataSource.SaveToObject($ado, "_Stream")
    $ado.SaveToFile($file, 1)
    $msg = ""
    $ado = ""
    Return True

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.

Share this post

Link to post
Share on other sites


What does this have to do with anything?

I want to add cookies to a CreateMHTMLBody-request...

Share this post

Link to post
Share on other sites


; 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

    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




Share this post

Link to post
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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.