Jump to content

Strange behaviour using WinHttp.WinHttpRequest ( Help )


Recommended Posts

Hello,

I'll get straight to the point, i'm facing a wierd problem when using WinHttp.WinHttpRequest Api and i want to set a header before making the http request look the code below

I start by defining my header string value

 Local $userAgent =  functionReturnsEncryptedBase64("mystrin"); // works
 ;; log $userAgent => correct output

When i pass the value as variable :

$cli.SetRequestHeader('X-User-Agent',$userAgent ) ;; the http call fails with the following error : ==> The requested action with this object has failed.

but when i pass the value to the SetRequestHeader function as a string literal strangely enough the http call works.

$cli.SetRequestHeader('X-User-Agent','my string encrypted') ;; works like a charm

i tried the following snippet but still it didn't work out

 

Local $header = StringFormat("X-User-Agent: %s",$userAgent)
 $cli.SetRequestHeader($header) ;; still no go ;'(

 

Anyone has an explanation ?

 

Have a good day :)

Link to comment
Share on other sites

31 minutes ago, argumentum said:

.. would you care to use WinHttp.au3 ?. I'm using it with all kind of unexpected/unorthodox header with out any problems. Full of variables.
Working fine from WinXP to Win11. I'm using it right now in a script.

i'm using winHttp.au3 too,

care to provide a snippet with header settings ?

Link to comment
Share on other sites

#include<WinHttp.au3>
#include<WinHttpConstants.au3>

#Include "Base64.au3"

Func _Base64WEncode2($string)
    Local $result = ""
    Local $arr = StringSplit($string, "")
    For $i = 1 To UBound($arr) - 1
        $result &= $arr[$i] & Chr(0)
    Next
    $result = _Base64Encode($result)
    Return $result
 EndFunc




    Local $cli = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $cli.Open("POST", "My site url", False)
    $cli.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

Local $userAgent =    _Base64WEncode2("mystring")

$cli.SetRequestHeader('X-User-Agent',$userAgent)

 $cli.Send($data) ;; data is passed from another function

 

Link to comment
Share on other sites

I suspect your open method fails. Do some checks.

Global Const $S_OK = 0x00000000

ObjEvent('AutoIt.Error', '__Error')

$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
ConsoleWrite('WinHttp object created: ' & (IsObj($oHTTP) ? True : False) & @CRLF)

$bRet = $oHTTP.Open('POST', 'https://microsoft.com', False)
ConsoleWrite(($bRet = $S_OK ? 'Open method successfully call.' : 'Open method failed') & @CRLF)

$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
ConsoleWrite(($bRet = $S_OK ? 'Set content type: OK' : 'Set content type: Error') & @CRLF)

$oHTTP.SetRequestHeader('X-User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36')
ConsoleWrite(($bRet = $S_OK ? 'Set user agent: OK' : 'Set user agent: Error') & @CRLF)

Func __Error($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
                        @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
                        @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
                        @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
                        @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
                        @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
                        @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
                        @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
                        @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
                        @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc

Console:

Quote

WinHttp object created: True
Open method successfully call.
Set content type: OK
Set user agent: OK

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

4 hours ago, Andreik said:

I suspect your open method fails. Do some checks.

Global Const $S_OK = 0x00000000

ObjEvent('AutoIt.Error', '__Error')

$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
ConsoleWrite('WinHttp object created: ' & (IsObj($oHTTP) ? True : False) & @CRLF)

$bRet = $oHTTP.Open('POST', 'https://microsoft.com', False)
ConsoleWrite(($bRet = $S_OK ? 'Open method successfully call.' : 'Open method failed') & @CRLF)

$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
ConsoleWrite(($bRet = $S_OK ? 'Set content type: OK' : 'Set content type: Error') & @CRLF)

$oHTTP.SetRequestHeader('X-User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36')
ConsoleWrite(($bRet = $S_OK ? 'Set user agent: OK' : 'Set user agent: Error') & @CRLF)

Func __Error($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
                        @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
                        @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
                        @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
                        @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
                        @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
                        @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
                        @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
                        @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
                        @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc

Console:

 

i tried this all OK, in your example you didn't pass user agent as variable, for me string literal works

Link to comment
Share on other sites

You didn't said what your variable contain. Shall we just guess?

Global Const $S_OK = 0x00000000

ObjEvent('AutoIt.Error', '__Error')

$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
ConsoleWrite('WinHttp object created: ' & (IsObj($oHTTP) ? True : False) & @CRLF)

$bRet = $oHTTP.Open('POST', 'https://microsoft.com', False)
ConsoleWrite(($bRet = $S_OK ? 'Open method successfully call.' : 'Open method failed') & @CRLF)

$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
ConsoleWrite(($bRet = $S_OK ? 'Set content type: OK' : 'Set content type: Error') & @CRLF)

$sAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'
$oHTTP.SetRequestHeader('X-User-Agent', $sAgent)
ConsoleWrite(($bRet = $S_OK ? 'Set user agent: OK' : 'Set user agent: Error') & @CRLF)

Func __Error($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
                        @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
                        @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
                        @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
                        @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
                        @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
                        @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
                        @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
                        @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
                        @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc

This works fine as well.

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

3 minutes ago, Andreik said:

You didn't said what your variable contain. Shall we just guess?

Global Const $S_OK = 0x00000000

ObjEvent('AutoIt.Error', '__Error')

$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
ConsoleWrite('WinHttp object created: ' & (IsObj($oHTTP) ? True : False) & @CRLF)

$bRet = $oHTTP.Open('POST', 'https://microsoft.com', False)
ConsoleWrite(($bRet = $S_OK ? 'Open method successfully call.' : 'Open method failed') & @CRLF)

$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
ConsoleWrite(($bRet = $S_OK ? 'Set content type: OK' : 'Set content type: Error') & @CRLF)

$sAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'
$oHTTP.SetRequestHeader('X-User-Agent', $sAgent)
ConsoleWrite(($bRet = $S_OK ? 'Set user agent: OK' : 'Set user agent: Error') & @CRLF)

Func __Error($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
                        @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
                        @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
                        @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
                        @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
                        @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
                        @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
                        @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
                        @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
                        @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc

This works fine as well.

Sorry the variable as show before contains a encoded base 64 string which i log just before the set request header and it shows up fine

Link to comment
Share on other sites

4 minutes ago, Andreik said:

You didn't said what your variable contain. Shall we just guess?

Global Const $S_OK = 0x00000000

ObjEvent('AutoIt.Error', '__Error')

$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
ConsoleWrite('WinHttp object created: ' & (IsObj($oHTTP) ? True : False) & @CRLF)

$bRet = $oHTTP.Open('POST', 'https://microsoft.com', False)
ConsoleWrite(($bRet = $S_OK ? 'Open method successfully call.' : 'Open method failed') & @CRLF)

$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
ConsoleWrite(($bRet = $S_OK ? 'Set content type: OK' : 'Set content type: Error') & @CRLF)

$sAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'
$oHTTP.SetRequestHeader('X-User-Agent', $sAgent)
ConsoleWrite(($bRet = $S_OK ? 'Set user agent: OK' : 'Set user agent: Error') & @CRLF)

Func __Error($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
                        @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
                        @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
                        @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
                        @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
                        @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
                        @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
                        @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
                        @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
                        @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc

This works fine as well.

here's an example of string  ; cABvAHcAZQByAF8ANABfADQANABCAEQAQgAxADYANwBcAEQARQBTAEsAVABPAFAALQBQADAAUgBFAEQANQA3AFwAYwA1AG4AXABNAGkAYwByAG8AcwBvAGYAdAAgAFcAaQBuAGQAbwB3AHMAIAAxADAAIABQAHIAbwAgADEAMAAuADAALgAxADkAMAA0ADUAWwBYADYANABdAFwAXABcAA==

Link to comment
Share on other sites

Not sure why would you send user agent encoded as base64 but can you confirm that this is not working?

Global Const $S_OK = 0x00000000

ObjEvent('AutoIt.Error', '__Error')

$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
ConsoleWrite('WinHttp object created: ' & (IsObj($oHTTP) ? True : False) & @CRLF)

$bRet = $oHTTP.Open('POST', 'https://microsoft.com', False)
ConsoleWrite(($bRet = $S_OK ? 'Open method successfully call.' : 'Open method failed') & @CRLF)

$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
ConsoleWrite(($bRet = $S_OK ? 'Set content type: OK' : 'Set content type: Error') & @CRLF)

$sAgent = 'cABvAHcAZQByAF8ANABfADQANABCAEQAQgAxADYANwBcAEQARQBTAEsAVABPAFAALQBQADAAUgBFAEQANQA3AFwAYwA1AG4AXABNAGkAYwByAG8AcwBvAGYAdAAgAFcAaQBuAGQAbwB3AHMAIAAxADAAIABQAHIAbwAgADEAMAAuADAALgAxADkAMAA0ADUAWwBYADYANABdAFwAXABcAA=='
$oHTTP.SetRequestHeader('X-User-Agent', $sAgent)
ConsoleWrite(($bRet = $S_OK ? 'Set user agent: OK' : 'Set user agent: Error') & @CRLF)

Func __Error($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
                        @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
                        @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
                        @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
                        @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
                        @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
                        @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
                        @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
                        @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
                        @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc

 

When the words fail... music speaks.

Link to comment
Share on other sites

1 minute ago, Andreik said:

Not sure why would you send user agent encoded as base64 but can you confirm that this is not working?

Global Const $S_OK = 0x00000000

ObjEvent('AutoIt.Error', '__Error')

$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
ConsoleWrite('WinHttp object created: ' & (IsObj($oHTTP) ? True : False) & @CRLF)

$bRet = $oHTTP.Open('POST', 'https://microsoft.com', False)
ConsoleWrite(($bRet = $S_OK ? 'Open method successfully call.' : 'Open method failed') & @CRLF)

$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
ConsoleWrite(($bRet = $S_OK ? 'Set content type: OK' : 'Set content type: Error') & @CRLF)

$sAgent = 'cABvAHcAZQByAF8ANABfADQANABCAEQAQgAxADYANwBcAEQARQBTAEsAVABPAFAALQBQADAAUgBFAEQANQA3AFwAYwA1AG4AXABNAGkAYwByAG8AcwBvAGYAdAAgAFcAaQBuAGQAbwB3AHMAIAAxADAAIABQAHIAbwAgADEAMAAuADAALgAxADkAMAA0ADUAWwBYADYANABdAFwAXABcAA=='
$oHTTP.SetRequestHeader('X-User-Agent', $sAgent)
ConsoleWrite(($bRet = $S_OK ? 'Set user agent: OK' : 'Set user agent: Error') & @CRLF)

Func __Error($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
                        @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
                        @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
                        @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
                        @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
                        @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
                        @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
                        @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
                        @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
                        @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc

 

yep this is not working, but if i pass the string literal instead of $sAgent it works :S

Link to comment
Share on other sites

2 minutes ago, Andreik said:

Anything else in the console? What version of AutoIt are you running?

Version 3.5.4 
    Mar 15 2018 13:15:52

console just after set request header with variable and not string literal : The requested action with this object has failed

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...