在Classic ASP中,从XML Formate中的服务器(API)获取请求

时间:2021-10-06 04:32:16

I need to take request from server url. Is that in XML format or not.

我需要从服务器URL获取请求。这是XML格式还是没有。

Set objServer = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    Set objXML       = Server.CreateObject("Msxml2.DOMDocument.3.0")

objerver.setTimeouts 30000, 30000, 30000, 30000
    objServer.Open "GET", "https://api.myurl/", False        
    objServer.setRequestHeader "Content-Type", "application/xml; charset=utf-8"
    objServer.setRequestHeader "Accept", "application/xml"

ObjServer.Send 

strXMLResponse = objServer.responseText
    blnXMLLoaded = objXML.loadXML(objServer.responseText)

If Not blnXMLLoaded Then
    aryReturn(0) = "XML failed to Load"
Else
    aryReturn(0) = "succssResult"
End If

When I attempt to run this experiment (with the broken/wrong URL) what I am getting is a run-time error. Because XMLLoaded to TRUE, but I want it to set it to FALSE. It is receiving back an HTML document, But here HTML also have some XML tags. so, though it is XML and the XMLLoaded flag is set to true, and so the error never gets thrown

当我尝试运行此实验(使用损坏/错误的URL)时,我得到的是运行时错误。因为XMLLoaded为TRUE,但我希望它将其设置为FALSE。它正在接收一个HTML文档,但这里的HTML也有一些XML标记。所以,尽管它是XML并且XMLLoaded标志设置为true,因此错误永远不会被抛出

Can any one help me please. Thanks, Jagadi

任何人都可以帮助我。谢谢,Jagadi

1 个解决方案

#1


1  

Your problem is that you have ignored the response status from the http request. You should at least be sure to test the status property is 200 before using responseText:

您的问题是您忽略了http请求的响应状态。在使用responseText之前,您至少应该确保测试status属性为200:

blnXMLLoaded = false
If objServer.status = 200 Then
    blnXMLLoaded = objXML.loadXML(objServer.responseText)
End If

The whole sequence I would actually code as:

我将实际编码的整个序列为:

Function GetXML(url)
    Dim xhr: Set xhr = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    xhr.setTimeouts 30000, 30000, 30000, 30000   
    xhr.Open "GET", url, False
    If xhr.status = 200 Then
        Set GetXML = xhr.responseXML
    Else
        Set GetXML = Nothing
    End If
End Function

Dim objXML : Set objXML = GetXML("https://api.myurl/")
If Not objXML Is Nothing Then
    ''# All is good stuff here
Else
    ''# Oops something bad happend stuff here
End If

#1


1  

Your problem is that you have ignored the response status from the http request. You should at least be sure to test the status property is 200 before using responseText:

您的问题是您忽略了http请求的响应状态。在使用responseText之前,您至少应该确保测试status属性为200:

blnXMLLoaded = false
If objServer.status = 200 Then
    blnXMLLoaded = objXML.loadXML(objServer.responseText)
End If

The whole sequence I would actually code as:

我将实际编码的整个序列为:

Function GetXML(url)
    Dim xhr: Set xhr = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    xhr.setTimeouts 30000, 30000, 30000, 30000   
    xhr.Open "GET", url, False
    If xhr.status = 200 Then
        Set GetXML = xhr.responseXML
    Else
        Set GetXML = Nothing
    End If
End Function

Dim objXML : Set objXML = GetXML("https://api.myurl/")
If Not objXML Is Nothing Then
    ''# All is good stuff here
Else
    ''# Oops something bad happend stuff here
End If