Jump to content

Set User Agent in winhttprequest - (Moved)


Recommended Posts

I'm trying to change the user agent of a winhttprequest request.

HttpsSetUserAgent("my user agent")

has not changed it.

$oHTTP.SetRequestHeader ("user-agent", "my user agent")

did not work either. The user agent always remained

Quote

Mozilla / 4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)

 

Link to comment
Share on other sites

1 hour ago, XGamerGuide said:

I'm trying to change the user agent of a winhttprequest request.

HttpsSetUserAgent("my user agent")

 

Maybe a typo ? HttpSetUserAgent  NOT HttpsSetUserAgent

EDIT :

Set a Com Errorhandler at the top of your script, e.g.

[...]
Global $oErrorHandler = ObjEvent("AutoIt.Error", "_MyCOMErrFunc") ; COM Errorhandler

[...]

Func _MyCOMErrFunc($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   ;==>_MyCOMErrFunc

 

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Quote

Set a Com Errorhandler at the top of your script, e.g.

Should it automatically call this function in the event of an error? I believe HttpSetUserAgent() only sets the user agent for InetRead() and InetGet().

Here is my code:

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
HttpSetUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41")
$oHTTP.Open("GET", "https://xgamerguide.sytes.net/test", False)
$oHTTP.SetRequestHeader("content-type", "text/html;charset=UTF-8")
$oHTTP.Send()

Here is what arrives on the server:

Quote

XX.XX.XX.XXX - - [06/Jun/2021:13:13:19 +0100] "GET /test HTTP/1.1" 404 169 "-" "Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)"

 

Link to comment
Share on other sites

3 hours ago, XGamerGuide said:

I believe HttpSetUserAgent() only sets the user agent for InetRead() and InetGet().

If that's what you believe, then why did you show an example using HttpSetUserAgent() that is not also using InetRead() or InetGet()?  :huh2:

 

12 hours ago, XGamerGuide said:

$oHTTP.SetRequestHeader ("user-agent", "my user agent")

did not work either.

Why didn't you show an example of your script that used the .SetRequestHeader method?  The syntax of the .SetRequestHeader method, that you used above, is correct and should have worked if the rest of the script was okay.

 

A simple search of this forum for "HttpSetUserAgent" OR ".SetRequestHeader" would have yielded NUMEROUS working examples.  None of those working examples would have shown the HttpSetUserAgent() function being used with a HTTP request COM object.  By the same token, none of those working examples would have shown the .SetRequestHeader method being used with a Inet* function.  So how exactly did you come up with your example script that you posted and the "yet unseen" example script that "did not work" using the .SetRequestHeader method?

Edited by TheXman
Link to comment
Share on other sites

  • Developers

Moved to the appropriate forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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