How can I download an url source html to a string in autohotkey v2

122 Views Asked by At

I'm new here, hope I'm doing things the right way!

I want to dl this URL, I've done it before (with help) but now I accidentally deleted my source, and I can't remember where I got help or what the answer was, stupid me. As it is all I get is status 406 (unacceptable)

I used the example from AHK help, but with a little modification, added 2 lines, as far as I recall something about SetRequestHeader

This is the example from help, url is modified


    req := ComObject("Msxml2.XMLHTTP")
    ; Open a request with async enabled.
    req.open("GET", "https://xhamsterlive.com/Lily_Young/profile", true)
    ; Set our callback function.
    req.onreadystatechange := Ready
    ; Send the request.  Ready() will be called when it's complete.
    req.send()
    /*
    ; If you're going to wait, there's no need for onreadystatechange.
    ; Setting async=true and waiting like this allows the script to remain
    ; responsive while the download is taking place, whereas async=false
    ; will make the script unresponsive.
    while req.readyState != 4
        sleep 100
    */
    Persistent

    Ready() {
        if (req.readyState != 4)  ; Not done yet.
            return
        if (req.status == 200) ; OK.
            MsgBox "Text: " req.responseText
        else
            MsgBox "Status " req.status,, 16
        ExitApp
    }

1

There are 1 best solutions below

0
Bobak On

Bard knew the answer, just had to ask the right way.

req.setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36')
req.setRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')