You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AutoHotkey v2下载URL HTML源码为字符串遇406错误求助

解决AutoHotkey请求网页返回406错误的问题

服务器返回406(不可接受)是因为它识别出你的请求并非来自常规浏览器,你需要添加请求头模拟浏览器行为,这就是你之前用过的SetRequestHeader的作用。

修改后的完整代码如下:

req := ComObject("Msxml2.XMLHTTP")
; 开启异步请求
req.open("GET", "https://xhamsterlive.com/Lily_Young/profile", true)
; 添加模拟浏览器的请求头
req.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
req.SetRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
; 设置回调函数
req.onreadystatechange := Ready
; 发送请求,完成后会调用Ready()
req.send()
/*
; 如果要等待请求完成,不需要onreadystatechange
; 设为async=true并这样等待,脚本在下载过程中仍能响应,而async=false会让脚本无响应
while req.readyState != 4
    sleep 100
*/
Persistent

Ready() {
    global req  ; 声明全局变量,否则函数无法访问外部的req
    if (req.readyState != 4)  ; 请求未完成则返回
        return
    if (req.status == 200) ; 请求成功
        MsgBox "Text: " req.responseText
    else
        MsgBox "Status " req.status,, 16
    ExitApp
}

关键说明:

  • User-Agent头告诉服务器你的请求来自Chrome浏览器,避免被识别为非浏览器请求
  • Accept头指定你能接受的内容类型,匹配服务器的返回规则
  • 必须在Ready函数里声明global req,否则函数内部无法访问外部定义的请求对象

内容的提问来源于stack exchange,提问作者Bobak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 23:55:28