; 下面说明有走代理访问的写法
NetIPAPI:=GetNetIPAPI()
MsgBox % "IP:" NetIPAPI[1] "`n地址:" NetIPAPI[2] "`n运营商:" NetIPAPI[3]
GetNetIPAPI(url:="http://whois.pconline.com.cn/ipJson.jsp?json=true") {
if ipobj:=UrlDownloadToVars(url,,,,,,,,,3){ ;设定超时时长
iJson:= Json_toObj(ipobj)
ipLocal:= iJson["pro"] iJson["city"] "-" iJson["addr"]
return [ iJson["ip"], ipLocal, ipLocal]
}
}
/*
;~ *****************说明*****************
;~ 此函数与内置命令 UrlDownloadToFile 的区别有以下几点
;~ 1.直接下载到变量,没有临时文件
;~ 2.下载速度更快,大概100%
;~ 3.支持超时,不必死等
;~ 4.内置命令执行时,整个AHK程序都是卡顿状态。此函数不会
;~ 5.内置命令下载一些诡异网站(例如“牛杂网”)时,会概率性让进程或线程彻底死掉。此函数不会
;~ 6.支持设置网页字符集、URL的编码,乱码问题轻松解决
;~ 7.支持设置Cookie、Referer、User-Agent,网站检测问题轻松解决
;~ 8.支持设置代理服务器
;~ 9.支持设置是否开启重定向
;~ 10.这个版本是 0.5
;~ *****************参数*****************
;~ URL 网址,必须包含类似“http://www.”的开头。
;~ Charset 网页字符集,不能是“936”之类的数字,必须是“gb2312”这样的字符。
;~ URLCodePage URL的编码,是“936”之类的数字,默认是“65001”。有些网站需要UTF-8,有些网站又需要gb2312
;~ Proxy 代理服务器,是形如“http://www.tuzi.com:80”的字符。 127.0.0.1:4780
;~ ProxyBypassList 代理服务器绕行名单,如 "*.microsoft.com" 的域名。符合条件,将不通过代理访问。多地址用;来分隔
;~ Cookie ,常用于登录验证。
;~ Referer 引用网址,常用于防盗链。
;~ UserAgent 用户信息,常用于防盗链。
;~ EnableRedirects 重定向,默认获取跳转后的页面信息,0为不跳转。
;~ Timeout 超时,单位为秒,默认不使用超时(Timeout=-1)。
*/
UrlDownloadToVars(URL,Charset="",URLCodePage="",Proxy="",ProxyBypassList="",Cookie="",Referer="",UserAgent="",EnableRedirects="",Timeout=-1) {
ComObjError(0) ; 禁用 COM 错误通告。禁用后,检查 A_LastError 的值,脚本可以实现自己的错误处理
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
if (URLCodePage<>"") ; 设置URL的编码
WebRequest.Option(2):=URLCodePage
if (EnableRedirects<>"")
WebRequest.Option(6):=EnableRedirects
if (Proxy<>"") ; 设置代理服务器。微软的代码 SetProxy() 是放在 Open() 之前的,所以我也放前面设置,以免无效
WebRequest.SetProxy(2,Proxy,ProxyBypassList)
WebRequest.Open("GET", URL, true) ; true为异步获取,默认是false。龟速的根源!!!卡顿的根源!!!
if (Cookie<>"") { ; 设置Cookie。SetRequestHeader() 必须 Open() 之后才有效
WebRequest.SetRequestHeader("Cookie","tuzi") ; 先设置一个cookie,防止出错,见官方文档
WebRequest.SetRequestHeader("Cookie",Cookie)
}
if (Referer<>"") ; 设置Referer
WebRequest.SetRequestHeader("Referer",Referer)
if (UserAgent<>"") ; 设置User-Agent
WebRequest.SetRequestHeader("User-Agent",UserAgent)
WebRequest.Send()
WebRequest.WaitForResponse(Timeout) ; WaitForResponse方法确保获取的是完整的响应
if (Charset="") ;设置字符集
return,RegExReplace(WebRequest.ResponseText(),"^\s+|\s+$")
else {
ADO:=ComObjCreate("adodb.stream") ; 使用 adodb.stream 编码返回值。参考 http://bbs.howtoadmin.com/ThRead-814-1-1.html
ADO.Type:=1 ;以二进制方式操作
ADO.Mode:=3 ;可同时进行读写
ADO.Open() ;开启物件
ADO.Write(WebRequest.ResponseBody()) ; 写入物件。注意 WebRequest.ResponseBody() 获取到的是无符号的bytes,通过 adodb.stream 转换成字符串string
ADO.Position:=0 ;从头开始
ADO.Type:=2 ;以文字模式操作
ADO.Charset:=Charset ;设定编码方式
return,RegExReplace(ADO.ReadText(),"^\s+|\s+$") ; 将物件内的文字读出
}
}
Json_toObj(ByRef src, args*) {
static q := Chr(34)
key := "", is_key := false
stack := [ tree := [] ]
is_arr := { (tree): 1 }
next := q . "{[01234567890-tfn"
pos := 0
while ( (ch := SubStr(src, ++pos, 1)) != "" ) {
if InStr(" `t`n`r", ch)
continue
if !InStr(next, ch, true) {
ln := ObjLength(StrSplit(SubStr(src, 1, pos), "`n"))
col := pos - InStr(src, "`n",, -(StrLen(src)-pos+1))
msg := Format("{}: line {} col {} (char {})"
, (next == "") ? ["Extra data", ch := SubStr(src, pos)][1]
: (next == "'") ? "Unterminated string starting at"
: (next == "\") ? "Invalid \escape"
: (next == ":") ? "Expecting ':' delimiter"
: (next == q) ? "Expecting object key enclosed in double quotes"
: (next == q . "}") ? "Expecting object key enclosed in double quotes or object closing '}'"
: (next == ",}") ? "Expecting ',' delimiter or object closing '}'"
: (next == ",]") ? "Expecting ',' delimiter or array closing ']'"
: [ "Expecting JSON value(string, number, [true, false, null], object or array)"
, ch := SubStr(src, pos, (SubStr(src, pos)~="[\]\},\s]|$")-1) ][1]
, ln, col, pos)
throw Exception(msg, -1, ch)
}
is_array := is_arr[obj := stack[1]]
if i := InStr("{[", ch) {
val := (proto := args[i]) ? new proto : {}
is_array? ObjPush(obj, val) : obj[key] := val
ObjInsertAt(stack, 1, val)
is_arr[val] := !(is_key := ch == "{")
next := q . (is_key ? "}" : "{[]0123456789-tfn")
}
else if InStr("}]", ch) {
ObjRemoveAt(stack, 1)
next := stack[1]==tree ? "" : is_arr[stack[1]] ? ",]" : ",}"
}
else if InStr(",:", ch) {
is_key := (!is_array && ch == ",")
next := is_key ? q : q . "{[0123456789-tfn"
}
else ; string | number | true | false | null
{
if (ch == q) { ; string
i := pos
while i := InStr(src, q,, i+1) {
val := StrReplace(SubStr(src, pos+1, i-pos-1), "\\", "\u005C")
static end := A_AhkVersion<"2" ? 0 : -1
if (SubStr(val, end) != "\")
break
}
if !i ? (pos--, next := "'") : 0
continue
pos := i ; update pos
val := StrReplace(val, "\/", "/")
, val := StrReplace(val, "\" . q, q)
, val := StrReplace(val, "\b", "`b")
, val := StrReplace(val, "\f", "`f")
, val := StrReplace(val, "\n", "`n")
, val := StrReplace(val, "\r", "`r")
, val := StrReplace(val, "\t", "`t")
i := 0
while i := InStr(val, "\",, i+1) {
if (SubStr(val, i+1, 1) != "u") ? (pos -= StrLen(SubStr(val, i)), next := "\") : 0
continue 2
; \uXXXX - JSON unicode escape sequence
xxxx := Abs("0x" . SubStr(val, i+2, 4))
if (A_IsUnicode || xxxx < 0x100)
val := SubStr(val, 1, i-1) . Chr(xxxx) . SubStr(val, i+6)
}
if is_key {
key := val, next := ":"
continue
}
}
else ; number | true | false | null
{
val := SubStr(src, pos, i := RegExMatch(src, "[\]\},\s]|$",, pos)-pos)
static null := "" ; for #Warn
if InStr(",true,false,null,", "," . val . ",", true) ; if var in
val := %val%
else if (Abs(val) == "") ? (pos--, next := "#") : 0
continue
val := val + 0, pos += i-1
}
is_array? ObjPush(obj, val) : obj[key] := val
next := obj==tree ? "" : is_array ? ",]" : ",}"
}
}
return tree[1]
}
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。

评论(0)