BORCANO Posted August 7, 2023 Posted August 7, 2023 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 :)
argumentum Posted August 7, 2023 Posted August 7, 2023 14 minutes ago, BORCANO said: but when i pass the value to the SetRequestHeader function as a string literal strangely enough the http call works. try String($userAgent) ? Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
BORCANO Posted August 7, 2023 Author Posted August 7, 2023 3 minutes ago, argumentum said: try String($userAgent) ? I already tried this, thanks.
argumentum Posted August 7, 2023 Posted August 7, 2023 .. 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. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
BORCANO Posted August 7, 2023 Author Posted August 7, 2023 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 ?
BORCANO Posted August 7, 2023 Author Posted August 7, 2023 #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
Andreik Posted August 7, 2023 Posted August 7, 2023 (edited) 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 August 7, 2023 by Andreik
argumentum Posted August 7, 2023 Posted August 7, 2023 ..and another question: why bas64 the agent ?, and why the "X-whatever" that's deprecated. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
argumentum Posted August 7, 2023 Posted August 7, 2023 1 hour ago, BORCANO said: i'm using winHttp.au3 too, Then you'd use the functions from the UDF. They also have a CHM somewhere in there ( the site ) with examples. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
BORCANO Posted August 7, 2023 Author Posted August 7, 2023 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
Andreik Posted August 7, 2023 Posted August 7, 2023 (edited) 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 August 7, 2023 by Andreik
BORCANO Posted August 7, 2023 Author Posted August 7, 2023 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
BORCANO Posted August 7, 2023 Author Posted August 7, 2023 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==
Andreik Posted August 7, 2023 Posted August 7, 2023 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
BORCANO Posted August 7, 2023 Author Posted August 7, 2023 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
Andreik Posted August 7, 2023 Posted August 7, 2023 Anything else in the console? What version of AutoIt are you running?
BORCANO Posted August 7, 2023 Author Posted August 7, 2023 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
Andreik Posted August 7, 2023 Posted August 7, 2023 You must live in future since latest AutoIt version it's 3.3.16.1. Just copy all the content of SciTE console and paste it here.
BORCANO Posted August 7, 2023 Author Posted August 7, 2023 3 minutes ago, Andreik said: You must live in future since latest AutoIt version it's 3.3.16.1. Just copy all the content of SciTE console and paste it here. lol,
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now