如何将 Global.asa 与 API 一起使用

How to use Global.asa with API

我正在尝试使用 API 到 return 网站访问者的纬度、经度、城市、州和邮政编码。我找到了一个我喜欢的 - GEO IPIFY

我的挑战是我的网站是用经典 ASP 完成的,API 的示例是用 C#、JAVA、Python 等编写的.

我想要实现的是在我的 global.asa 中获取所需的值,lat、lng、zip 等,并将它们设置为会话变量。以下示例适用于 VB.net.

Imports System
Imports System.Net
Imports System.IO

Class Program
    Public Const IP As String = "63.148.239.195"
    Public Const API_KEY As String = "at_6ZsRWjq..."
    Public Const API_URL As String = "https://geo.ipify.org/api/v1?"

    Private Shared Sub Main()
        Dim url As String = API_URL & $"apiKey={API_KEY}&ipAddress={IP}"
        Dim resultData As String = String.Empty
        Dim req As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
        Using response As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)
            Using stream As Stream = response.GetResponseStream()
                Using reader As StreamReader = New StreamReader(stream)
                    resultData = reader.ReadToEnd()
                End Using
            End Using
        End Using
        Console.WriteLine(resultData)
    End Sub
End Class

Global.asa 有一个 Session_OnStart 子,但是您在 Global.asa 文件中可以做的事情非常有限,所以尝试调用 API Session_OnStart 会导致问题。您也不能在 Global.asa 之外使用 Session_OnStart 或从 Global.asa 中调用任何函数或子例程,除非 functions/subs 也被编码为 Global.asa。 =20=]

我喜欢做的是创建一个 global.asp 文件,我用它来设置各种应用程序设置,包括站点范围 类 并存储我整个过程中需要的重要功能和子例程地点。然后我在我的 asp 网站的所有页面上包含 global.asp。在 global.asp 中,您可以设置一个调用 ipify.org api 的子程序,并在 JSON class 的帮助下将结果存储为会话变量。如果 api 在用户会话期间已被调用,则您可以在每个页面加载时调用子并退出子,或者如果是新会话则进行新调用。

global.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%

    ' Set some useful application settings, constants, other site-wide classes etc...

    Server.ScriptTimeout = 20
    Session.Timeout = 720
    Response.Charset = "UTF-8"
    Response.LCID = 1033

%>
<!--#include virtual = "/classes/jsonObject.class.asp" -->
<%

    Sub ipify()

        ' Only call the ipify API once per session (but try 3 times)
        ' If session("ipify_called") is true or 3+ attempts have been  
        ' made to call the API then exit the sub

        if session("ipify_called") OR session("ipify_attempts") >= 3 then exit sub

        ' Assign a value of 0 to ipify_attempts if this is a new session

        if isEmpty(session("ipify_attempts")) then session("ipify_attempts") = 0

        Const api_key = "YOUR_API_KEY"
        Const api_url = "https://geo.ipify.org/api/v1"

        Dim rest : Set rest = Server.CreateObject("MSXML2.ServerXMLHTTP")
        rest.open "GET", api_url, False
        rest.send "apiKey=" & api_key & "&ipAddress=" & Request.ServerVariables("REMOTE_ADDR")

        if rest.status = 200 then

            Dim JSON : Set JSON = New JSONobject
            Dim oJSONoutput : Set oJSONoutput = JSON.Parse(rest.responseText)

                if isObject(oJSONoutput("location")) then

                    session("ipify_country") = oJSONoutput("location")("country")
                    session("ipify_region") = oJSONoutput("location")("region")
                    session("ipify_city") = oJSONoutput("location")("city")
                    session("ipify_lat") = oJSONoutput("location")("lat")
                    session("ipify_lng") = oJSONoutput("location")("lng")
                    session("ipify_postalCode") = oJSONoutput("location")("postalCode")
                    session("ipify_timezone") = oJSONoutput("location")("timezone")

                    ' To prevent the api from being called again during this session

                    session("ipify_called") = true

                else

                    ' oJSONoutput("location") should be an object, but it isn't.
                    ' The rest.responseText is probably invalid

                    session("ipify_attempts") = session("ipify_attempts") + 1

                end if

            Set oJSONoutput = nothing
            Set JSON = nothing

        else

            ' Unexpected status code, probably a good idea to log the rest.responseText

            session("ipify_attempts") = session("ipify_attempts") + 1

        end if

        set rest = nothing

    End Sub


    ' Call the ipify sub on each page load.

    Call ipify()

%>

JSON Class 使用: https://github.com/rcdmk/aspJSON

请务必在 asp 网站的其他页面上包含 global.asp:

<!--#include virtual = "/global.asp" -->