Time management of a script asp - asp-classic

within a page ASP (classic) I have a function that connects to the server via XMLHTTP to google to get the time and distance between two points.
I noticed that sometimes my server makes this request very slowly sending the script out and blocking the page.
I would therefore ask you.
If I change the attached script to say
if the execution time exceeds five seconds then stops the script
function GooDistInd(origine,destintario)
Set objxml = Nothing
' Dichiaro le variabili che mi servono nello script
Dim file, objXmlHttp, objXmlDom, distanza, cognome, i
'http://maps.google.com/maps/api/directions/xml?origin=40.7143528,-74.0059731&destination=40.7035458,-74.21607971&sensor=false
file = "http://maps.googleapis.com/maps/api/directions/xml?origin="& origine &"destination="&destintario &"&sensor=false"
Set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
objXmlHttp.Open "GET", file, False
objXmlHttp.Send
Set objXmlDom = Server.CreateObject("Microsoft.XMLDOM")
objXmlDom.async = False
objXmlDom.loadXML(objXmlHttp.responseText)
Set tempo = objXmlDom.getElementsByTagName("leg/distance/value")
i = 0
For i = 0 To tempo.length - 1
distanza=tempo(i).Text
exit for
Next
GooDistInd =distanza
end function

If you want to stop the script completely you could use
Server.ScriptTimeout = 5
You could also use the timeut property of the Microsoft.XMLHTTP object and set it to 5000 ms. In this case you could work with on error resume next and check if an error occurred, so you could give back a significant error message to you users

Related

How can I clear all the SAP GUI fields when I open a T-Code?

When I open a T-Code from SAP GUI, some of the fields are pre-populated from past queries. Is it possible to enter a T-Code and all the fields in the next window to be forced blank?
I develop scripts for SAP GUI and run into problems if fields already have content from prior queries.
The history cannot be disabled user-wise. Period.
Either all or nobody.
If you want to disable the history go to SAPgui options into Local data setting
The history in Windows is a simple Access MDB file but it is password-protected, so you may try to crack it and delete only your user lines but it is a bunch of work.
However, I guess the history that makes you crazy is not what I described above, but SPA/GPA parameters. Check it first
You can empty some fields with :
""
session.findById("wnd[1]/usr/ctxtRMMG1_REF-BWTAR").Text = ""
But doesn't work all the time...
My method for this is to loop through all the fields (and recursively on all children elements) and set the text value of them to an empty string.
On read-only fields it throws an error and this is why "On Error Resume Next" is necessary.
Sub Start_Clearing()
'setup SAP
If Not IsObject(SAPApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPApplication.Children(0)
End If
If Not IsObject(Session) Then
Set Session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject Session, "on"
WScript.ConnectObject SAPApplication, "on"
End If
Dim UserArea As Object
Set UserArea = Session.findByID("wnd[0]/usr")
Clear_Fields UserArea
End Sub
'_______________________________
Sub Clear_Fields(Area As Object)
Set SAPApplication = GetObject("SAPGUI").GetScriptingEngine
Dim Obj As Object
Dim NextArea As Object
On Error Resume Next
For i = 0 To Area.Children.Count - 1
Set Obj = Area.Children(CInt(i))
If Obj.ContainerType = True Then
If Obj.Children.Count > 0 Then
Set NextArea = SAPApplication.findByID(Obj.ID)
Clear_Fields NextArea
End If
End If
Obj.Text = ""
Next i
End Sub

Database Editing in Session OnEnd Classic ASP

I was Creating a website in Classic ASP Project, having a Logout feature.
I want to access the login database and make changes, when the session ends. The coding is something like:
In global.asa :
Sub Session_OnEnd()
Set studcon = Server.CreateObject("ADODB.Connection")
studcon.Open "Provider = Microsoft.JET.OLEDB.4.0;Data Source = E:\mailfan.mdb"
Set studrec = Server.CreateObject("ADODB.Recordset")
studrec.Open "login", studcon, 1, 3
studrec.movefirst
found = 0
Do While studrec.EOF or found = 1
If studrec("ID") = Session("uid") Then
studrec("log") = 0
studrec.Update
End If
studrec.movenext
Loop
studrec.close
studcon.close
Set studrec = Nothing
Set studcon = Nothing
End Sub
But Even After Session.Abandon the value of log field Remains the same (It is 1 when session's active).
Small Update: I tried to google it, and did recieve a very similar link having the same question: http://www.justskins.com/forums/problem-with-global-asa-77015.html
But the code is the same as suggested by Aaron Bertrand in the above link. Is it due to IIS 8.0? Because I would be Running it on Win XP sp3 IIS 6. And I Do require it to work there.
I can't see an SQL query anywhere in your code. If you're intent on using a recordset for your update then in place of
studrec.Open "login", studcon, 1, 3
you should have something like
studrec.Open "select * from login", studcon, 1, 3
This assumes that your database table is called "Login"
Using a recordset is actually an over complex means of doing all this though, An Update query should suffice. You could remove all your code from
Set studrec = Server.CreateObject("ADODB.Recordset")
to
studrec.close
and replace it with
studcon.Execute("UPDATE Login set log = 0 where id = " & Session("uid"))

Not getting attribute from a xml file into asp

I have the following xml result from this link - https://api.eveonline.com/eve/CharacterID.xml.aspx?names=BorisKarlov
<eveapi version="2">
<currentTime>2013-01-16 18:57:38</currentTime>
<result>
<rowset name="characters" key="characterID" columns="name,characterID">
<row name="BorisKarlov" characterID="315363291"/>
</rowset>
</result>
<cachedUntil>2013-02-16 18:57:38</cachedUntil>
</eveapi>
and I am trying to extract the characterID into asp. I am using the following code,
Set oXML = Server.CreateObject("Msxml2.DOMDocument.6.0")
oXML.LoadXML("https://api.eveonline.com/eve/CharacterID.xml.aspx?names=BorisKarlov")
Set oRoot = oXML.selectSingleNode("//result")
For Each oNode In oRoot.childNodes
response.Write oNode.Attributes.getNamedItem("characterID").Text
Next
Set oXML = Nothing
All i keep getting is the following error:
Microsoft VBScript runtime error '800a01a8'
Object required: 'oRoot'
.............
I can only assume that Set oRoot = oXML.selectSingleNode("//result") is not actually generating any data and therefore throwing up the error in the next line.
Can anyone please shed some light on my problem?
You have a few problems there.
loadXML() is for loading a block of XML as a string, not fetching from a remote server; for that, you need to use load()
when loading from a server, you need to tell it to use the ServerXMLHTTP component, and set async to false so that it waits until loaded before executing the rest of your script.
when I tried loading that XML, I got an encoding error; you will need to resolve that one way or another
when I loaded the XML directly from a string, it wouldn't parse because there is a script element containing non-XML content; that needs to be contained within a CDATA section
your XPath query is to //result, but you actually need it to be //result/rowset
This code should work once you resolve issues 3 and 4 above:
Set oXML = Server.CreateObject("Msxml2.DOMDocument.6.0")
oXML.async = False
oXML.setProperty "ServerHTTPRequest", true
oXML.Load("https://api.eveonline.com/eve/CharacterID.xml.aspx?names=BorisKarlov")
If oXML.parseError.errorCode <> 0 Then
Response.Write "<p>XML parse error: " & Server.HTMLEncode(oXML.parseError.reason) & "</p>"
Else
Set oRoot = oXML.selectSingleNode("//result/rowset")
If oRoot Is Nothing Then
response.write "Nothing!"
response.end
End If
For Each oNode In oRoot.childNodes
response.Write oNode.Attributes.getNamedItem("characterID").Text
Next
End If
Set oXML = Nothing
Edit: to get around the problem #3, and oddly also #4 (don't know why!), use this snippet to load the XML instead. For some reason, I think the code above isn't handling the gzip compressed stream correctly, but this code below does.
Set oXML = Server.CreateObject("Msxml2.DOMDocument.6.0")
Set xh = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
xh.open "GET", "https://api.eveonline.com/eve/CharacterID.xml.aspx?names=BorisKarlov", False
xh.send
xml = xh.responseText
oXML.LoadXML xml

What if TinyURL API doesn't work..?

I have a vbscript function to create a tinyurl from a regular url.
FUNCTION GetShortURL(strUrl)
Dim oXml,strTinyUrl,strReturnVal
strTinyUrl = "http://tinyurl.com/api-create.php?url=" & strUrl
set oXml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXml.Open "GET", strTinyUrl, false
oXml.Send
strReturnVal = oXml.responseText
Set oXml = nothing
GetShortURL = strReturnVal
END FUNCTION
I have come across the problem when the tinyurl api is down or inaccessible, making my script fail:
msxml3.dll
error '80072efe'
The connection with the server was terminated abnormally
Is there a safeguard I can add to this function to prevent the error and use the long url it has..?
Many thanks in advance,
neojakey
If you want to just return strUrl if the call fails, you can use On Error Resume Next
FUNCTION GetShortURL(strUrl)
on error resume next
Dim oXml,strTinyUrl,strReturnVal
strTinyUrl = "http://tinyurl.com/api-create.php?url=" & strUrl
set oXml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXml.Open "GET", strTinyUrl, false
oXml.Send
strReturnVal = oXml.responseText
Set oXml = nothing
'Check if an error occurred.
if err.number = 0 then
GetShortURL = strReturnVal
else
GetShortURL = strUrl
end if
END FUNCTION
Obviously, you need some kind of error handling. In general, VBScript's error handling is no fun, but for your specs - get the right thing or the default in case of any/don't care what problem - you can use a nice and simple approach:
Reduce your original function to
assignment of default value to function name (to prepare for the failures)
On Error Resume Next (to disable VBScript's default error handling, i.e. crash)
assignment of the return value of a helper function to function name (to prepare for success)
In code:
Function GetShortURL2(strUrl)
GetShortURL2 = strUrl
On Error Resume Next
GetShortURL2 = [_getshorturl](GetShortURL2)
End Function
I use [] to be able to use an 'illegal' function name, because I want to emphasize that _getshorturl() should not be called directly. Any error in _getshorturl() will cause a skip of the assignment and a resume on/at the next line (leaving the function, returning the default, reset to default error handling).
The helper function contains the 'critical parts' of your original function:
Function [_getshorturl](strUrl)
Dim oXml : Set oXml = CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXml.Open "GET", "http://tinyurl.com/api-create.php?url=" & strUrl, False
oXml.Send
[_getshorturl] = oXml.responseText
End Function
No matter which operation fails, the program flow will resume on/at the "End Function" line of GetShortURL2().
Added: Comments on the usual way to do it (cf. Daniel Cook's proposal)
If you switch off error handling (and "On Error Resume Next" is just that) you are on thin ice: all errors are hidden, not just the ones you recon with; your script will do actions in all the Next lines whether necessary preconditions are given or preparations done.
So you should either restrict the scope to one risky line:
Dim aErr
...
On Error Resume Next
SomeRiskyAction
aErr = Array(Err.Number, Err.Description, Err.Source)
On Error Goto 0
If aErr(0) Then
deal with error
Else
normal flow
End If
or check after each line in the OERN scope
On Error Resume Next
SomeRiskyAction1
If Err.Number Then
deal with error
Else
SomeRiskyAction2
If Err.Number Then
deal with error
Else
...
End If
End If
That's what I meant, when I said "no fun". Putting the risky code in a function will avoid this hassle.

Getting "Microsoft VBScript runtime error '800a01a8'"

I had inherited a website to do general maintenance on. The web pages are ASP pages but the bulk of my work has been with the html/css. The site has been working fine for a number of years, that is until now. When I try to load pages that are containing VB script I receive the following error:
Microsoft VBScript runtime error '800a01a8'
Object required: '[object]'
/new_products.asp, line 12
the name of the asp page changes based on the page that is being called, but the line reference is the same.
The code that is generating the error is as follows:
Dim objXMLHTTP
Dim coordinates
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
ip = Request.ServerVariables("REMOTE_ADDR")
objXMLHTTP.Open "GET", "http://www.ippages.com/xml/?ip="&ip&"&get=country", False
objXMLHTTP.Send
Set xmldoc = objXMLHTTP.responseXML
country = xmldoc.selectSingleNode("ip_address/lookups/lookup_country").text
set objXMLHTTP=nothing
theCountry = Trim(UCASE(country))
If theCountry = "CA-CANADA" then
Bags = "13.99"
SB4Pack = "14.00"
currency_code = "CAD"
lc = "CA"
Pref = "$"
ShipType = 1
ElseIf theCountry = "US-UNITED STATES" then
Bags = "13.99"
SB4Pack = "14.00"
currency_code = "USD"
lc = "US"
Pref = "$"
ShipType = 2
Else
Bags = "8.00"
SB4Pack = "11.00"
currency_code = "GBP"
lc = "UK"
Pref = "£"
ShipType = 2
End If
With my extremely limited knowledge of VB I have done my best to try and figure out why the code has stopped working, but I am stumped. Any and all help is greatly greatly appreciated.
Since this is an environment specific issue which started to popup recently as you mentioned... No sure statement can be provided. You need to investigate it. What one can do is to suggest some thing. I believe your problem is one of this statement.
Set xmldoc = objXMLHTTP.responseXML
country = xmldoc.selectSingleNode("ip_address/lookups/lookup_country").text
Probably this value is not coming or Null or Nothing.Try to print the value of country if it's coming as Null, Nothing... Later when you try to change the case of country.... You can use tools to capture the Http request and response tools like Fidler to verify what is coming in lookup_country as a value or whether you are getting this node or not.
Also where is theCountry is declared. If it's dynamic variable then check if some one enabled OptionExplicit??

Resources