LOGO 首页 OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 技术文档 其他文档  
 
网站管理员

ASP 经典版 获取 IP 归属地(调用 https://ip9.com.cn 接口)

admin
2026年9月10日 16:24 本文热度 146

说明:使用 ASP(VBScript),调用接口 https://ip9.com.cn/get?ip=目标IP 获取 IP 的国家、省、市、区县、运营商信息;

经典 ASP 环境,需要服务器支持 MSXML2.XMLHTTP / WinHttp.WinHttpRequest.5.1

服务器需要能够外网访问ip9.com.cn

1、完整 asp 代码 IpLocation.asp

<%
Option Explicit
'=============================================
' 函数:GetIpLocation
' 输入:ip 要查询的IP地址
' 返回:字典对象,包含 ret,ip,country,prov,city,area,isp 等字段
'=============================================
Function GetIpLocation(ByVal ip)
    Dim objHttp, url, strJson, dic
    Set dic = Server.CreateObject("Scripting.Dictionary")
    
    '初始化默认值
    dic("ret") = 0
    dic("ip") = ip
    dic("country") = ""
    dic("prov") = ""
    dic("city") = ""
    dic("area") = ""
    dic("isp") = ""
    
    If Trim(ip) = "" Then
        Set GetIpLocation = dic
        Exit Function
    End If
    
    url = "https://ip9.com.cn/get?ip=" & Server.URLEncode(ip)
    
    On Error Resume Next
    '优先使用WinHttp,支持https
    Set objHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
    If Err.Number<>0 Then
        Err.Clear
        Set objHttp = Server.CreateObject("MSXML2.XMLHTTP.6.0")
    End If
    On Error GoTo 0
    
    objHttp.SetTimeouts 5000,5000,5000,5000 '超时5秒
    objHttp.Open "GET", url, False
    objHttp.Send
    
    If objHttp.Status = 200 Then
        strJson = objHttp.ResponseText
        '解析JSON,ASP无内置JSON,使用简易解析函数
        Call ParseIp9Json(strJson, dic)
    End If
    
    Set objHttp = Nothing
    Set GetIpLocation = dic
End Function
'=============================================
' 简易JSON解析,适配ip9.com.cn返回结构
' 输入json文本,输出填充到dic字典
' 接口返回格式示例:
' {"ret":200,"data":{"ip":"71.132.20.76","country":"中国","prov":"北京","city":"北京","area":"","isp":"Amazon"}}
'=============================================
Sub ParseIp9Json(ByVal jsonStr, ByRef dic)
    Dim reg, matches, m
    Set reg = New RegExp
    reg.Global = True
    reg.IgnoreCase = True
    
    '匹配ret
    reg.Pattern = """ret""\s*:\s*(\d+)"
    Set matches = reg.Execute(jsonStr)
    If matches.Count>0 Then dic("ret") = CInt(matches(0).SubMatches(0))
    
    '匹配data内部字段
    reg.Pattern = """ip""\s*:\s*""([^""]*)"""
    Set matches = reg.Execute(jsonStr)
    If matches.Count>0 Then dic("ip") = matches(0).SubMatches(0)
    
    reg.Pattern = """country""\s*:\s*""([^""]*)"""
    Set matches = reg.Execute(jsonStr)
    If matches.Count>0 Then dic("country") = matches(0).SubMatches(0)
    
    reg.Pattern = """prov""\s*:\s*""([^""]*)"""
    Set matches = reg.Execute(jsonStr)
    If matches.Count>0 Then dic("prov") = matches(0).SubMatches(0)
    
    reg.Pattern = """city""\s*:\s*""([^""]*)"""
    Set matches = reg.Execute(jsonStr)
    If matches.Count>0 Then dic("city") = matches(0).SubMatches(0)
    
    reg.Pattern = """area""\s*:\s*""([^""]*)"""
    Set matches = reg.Execute(jsonStr)
    If matches.Count>0 Then dic("area") = matches(0).SubMatches(0)
    
    reg.Pattern = """isp""\s*:\s*""([^""]*)"""
    Set matches = reg.Execute(jsonStr)
    If matches.Count>0 Then dic("isp") = matches(0).SubMatches(0)
    
    Set reg=Nothing
End Sub
'=============================================
' 获取客户端真实IP函数(ASP获取访问者IP)
'=============================================
Function GetClientIP()
    Dim ip
    ip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If ip = "" Then ip = Request.ServerVariables("REMOTE_ADDR")
    '处理多代理逗号分割取第一个
    If InStr(ip,",")>0 Then ip = Split(ip,",")(0)
    GetClientIP = Trim(ip)
End Function
'===================== 调用示例 =====================
Dim clientIP, loc
clientIP = GetClientIP()
Set loc = GetIpLocation(clientIP)
Response.Write "你的IP:" & loc("ip") & "<br/>"
Response.Write "国家:" & loc("country") & "<br/>"
Response.Write "省份:" & loc("prov") & "<br/>"
Response.Write "城市:" & loc("city") & "<br/>"
Response.Write "区县:" & loc("area") & "<br/>"
Response.Write "运营商:" & loc("isp") & "<br/>"
'拼接完整地址
Dim fullAddr
fullAddr = loc("country") & " " & loc("prov") & " " & loc("city") & " " & loc("area")
Response.Write "完整地址:" & Trim(fullAddr)
Set loc=Nothing
%>

2、接口返回 JSON 样例参考

{"ret":200,"data":{"ip":"71.132.20.76","country":"中国","prov":"北京","city":"北京","area":"","isp":"Amazon"}}

3、关键说明 & 坑点

  1. HTTPS 支持:经典 ASP MSXML2.XMLHTTP 老版本不支持 HTTPS,优先用 WinHttp.WinHttpRequest.5.1;服务器组件没有注册会报错。

  2. 外网权限:ASP 是服务端调用接口,服务器必须可以访问外网ip9.com.cn,本地浏览器能访问不等于服务器可以访问。

  3. JSON 解析:ASP 没有内置 JSON 解析库,上面用正则简易解析,只适配ip9.com.cn返回格式,不要用于其他 json 接口;生产环境建议使用第三方 ASP JSON 类。

  4. 缓存建议:公网 IP 归属地查询不要高频调用第三方 API,可以把查询结果缓存到 Application 或者数据库,减少外部接口请求。

  5. 多代理获取真实客户端 IPGetClientIP()处理反向代理、CDN 场景,优先读取HTTP_X_FORWARDED_FOR

4、如果需要第三方完整 JSON 解析(可选增强)

上面正则解析够用,但如果接口字段变化会解析失败,可以引入经典 ASP 常用的JSON.asp解析库,把返回的ResponseText直接反序列为对象。

调用示例(引入 JSON.asp 后)

<!--#include file="JSON.asp"-->
Dim jsonObj,str
str = objHttp.ResponseText
Set jsonObj=JSON.parse(str)
Response.Write jsonObj.data.country
Response.Write jsonObj.data.prov
Response.Write jsonObj.data.city
Response.Write jsonObj.data.area
Response.Write jsonObj.data.isp

5、封装成函数直接调用

'查询指定IP
Set loc = GetIpLocation("114.114.114.114")
Response.Write loc("prov") & loc("city")

⚠️注意:ip9.com.cn​是免费公开接口,请勿高频批量刷接口,否则会被限制访问。

相关教程:

1、JSON.asp 经典 ASP JSON 解析类(VBScript,经典 ASP 可用)[40848]
  http://40848.oa22.cn


该文章在 2026/9/10 16:25:35 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2026 ClickSun All Rights Reserved  粤ICP备13012886号-9  粤公网安备44030602007207号