Vbscript 如何将Global.asa与API一起使用
我试图使用API返回网站访问者的纬度、经度、城市、州和邮政编码。我找到了一个我喜欢的- 我面临的挑战是,我的网站是用经典的ASP完成的,API的示例是用C、JAVA、Python等 我试图实现的是在我的global.asa中获取所需的值lat、lng、zip等,并将它们设置为会话变量。下面的示例适用于VB.netVbscript 如何将Global.asa与API一起使用,vbscript,asp-classic,Vbscript,Asp Classic,我试图使用API返回网站访问者的纬度、经度、城市、州和邮政编码。我找到了一个我喜欢的- 我面临的挑战是,我的网站是用经典的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 St
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文件中可以做的事情非常有限,因此尝试从Session_OnStart调用API会导致问题。您也不能在Global.asa之外使用Session_OnStart,也不能从Global.asa内部调用任何函数或子例程,除非这些函数/子例程也被编码到Global.asa中
我喜欢做的是创建一个global.asp文件,我用它来设置各种应用程序设置,包括站点范围的类,并存储整个站点所需的重要函数和子例程。然后,我在asp站点的所有页面上都包含global.asp。在global.asp中,您可以设置一个子系统,该子系统调用ipify.org api,并将结果存储为会话变量,并借助于。然后,您可以在每次页面加载时调用sub,如果api在用户会话期间已被调用,则退出sub;如果是新会话,则进行新调用
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类:
请确保在asp站点的其他页面上包含global.asp:
经典ASP中的global.asa文件使用的是VBScript而不是VB.Net,因此不确定您想做什么。回答得很好,但您觉得这是在浪费时间,因为这个问题根本不值得。他们发布的代码直接来自于,他们自己甚至没有尝试过尝试,现在你只是验证了他们不尝试的决定。@Lankymart看起来是这样的:@TMHDesign可能是真的。对不起,Lankymart,但他在这里也帮助了很多人。在发布问题时,通常有礼貌地概述您为使代码正常工作已经采取的步骤,并提供示例供其他开发人员查看,并尝试找出您的错误所在。它并不是真的想成为一个我该怎么做?类型论坛,如果你花时间研究的话,有很多关于这类事情的在线教程。尽管说了这么多,你的问题还是提出了一个有趣的挑战,我很乐意回答。我很高兴我能帮上忙:@Adam当你觉得问题具有挑战性时,你会继续跳到每一个问题上,而不考虑OP的努力/质量。一旦不再是挑战,你就会失去兴趣,意识到一遍又一遍地回答同样的问题是毫无意义的。@TMHDesign也许这就是困惑的地方/误解是,这不是一个“新手”的网站。它是程序员为程序员设计的标准资源,如果您刚开始使用特定的编程/脚本语言,它不能替代一本好的教程或书籍。