在经典 asp 中读取和显示网络服务输出
reading and displaying a webservice output in classic asp
我正在阅读经典 asp 的网络服务输出。
Web服务输出如下。
<boolean xmlns="http://somewebsite.com/">true</boolean>
这是预期的正确输出。
我写了下面的代码来读取经典 asp 中的输出。
Set obj1 = Server.createobject("MSXML2.ServerXMLHTTP.3.0")
URL1 = "http://webserive.asmx/method?para=2"
obj1.open "GET", URL1, False
obj1.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=utf-8"
obj1.setRequestHeader "SOAPAction", URL1
obj1.send
if obj1.responseText <> "" Then
response.write "ok." & obj1.responseText
end if
但此输出打印以下输出:
"ok. true"
输出中有一个 space 不是预期的。
这就是问题所在。
请指教
就 Internet 浏览器而言,您从 ResponseText
的所有强烈和目的输出都是有效的 HTML 结构,并将相应地对待它。当您使用 Response.Write()
将内容发送到浏览器时,它被发送 "as is" 所以在这种情况下 <boolean>
元素被视为 HTML 所以只有包含的文本 true
输出。
要解决此问题,您首先需要在将 ResponseText
发送到浏览器之前对 HTML 进行编码,以便浏览器知道将发送的内容视为纯旧文本。您可以通过调用方法 Server.HTMLEncode()
来完成此操作
Response.Write "ok." & Server.HTMLEncode(obj1.ResponseText)
根据MSDN;
The HTMLEncode method applies HTML encoding to a specified string. This is useful as a quick method of encoding form data and other client request data before using it in your Web application. Encoding data converts potentially unsafe characters to their HTML-encoded equivalent.
If the string to be encoded is not Double-Byte Character Set (DBCS), HTMLEncode converts characters as follows:
- The less-than character (
<
) is converted to <
.
- The greater-than character (
>
) is converted to >
.
- The ampersand character (
&
) is converted to &
.
- The double-quote character (
"
) is converted to "
.
- Any ASCII code character whose code is greater-than or equal to
0x80
is converted to &#<number>
, where <number>
is the ASCII character value.
If the string to be encoded is DBCS, HTMLEncode converts characters as follows:
- All extended characters are converted.
- Any ASCII code character whose code is greater-than or equal to
0x80
is converted to &#<number>
, where <number>
is the ASCII character value.
- Half-width Katakana characters in the Japanese code page are not converted.
在发送 ResponseText
的那一刻发生了这种情况;
响应文本
<boolean xmlns="http://somewebsite.com/">true</boolean>
客户端输出
ok.true
如果您使用 Server.HTMLEncode()
它将是;
响应文本 (HTML 已编码)
<boolean xmlns="e;http://somewebsite.com/"e;>true</boolean>
客户端输出
ok.<boolean xmlns="http://somewebsite.com/">true</boolean>
所以我想我可能没有理解您的需求。如果要解析 XML 以将其删除,则仅包含;
的内容
<boolean xmlns="http://somewebsite.com/">true</boolean>
在这种情况下 返回给浏览器 true
那么您需要使用 XPath 之类的东西解析 XML 以获取基础值。
'After the initial Send()
Dim xml, root
If obj1.Status = 200 Then
Set xml = obj1.ResponseXML
Call xml.SetProperty("SelectionLanguage", "XPath")
Set root = xml.DocumentElement
Call Response.Write(root.SelectSingleNode("boolean").Text)
End If
我正在阅读经典 asp 的网络服务输出。
Web服务输出如下。
<boolean xmlns="http://somewebsite.com/">true</boolean>
这是预期的正确输出。
我写了下面的代码来读取经典 asp 中的输出。
Set obj1 = Server.createobject("MSXML2.ServerXMLHTTP.3.0")
URL1 = "http://webserive.asmx/method?para=2"
obj1.open "GET", URL1, False
obj1.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=utf-8"
obj1.setRequestHeader "SOAPAction", URL1
obj1.send
if obj1.responseText <> "" Then
response.write "ok." & obj1.responseText
end if
但此输出打印以下输出:
"ok. true"
输出中有一个 space 不是预期的。
这就是问题所在。
请指教
就 Internet 浏览器而言,您从 ResponseText
的所有强烈和目的输出都是有效的 HTML 结构,并将相应地对待它。当您使用 Response.Write()
将内容发送到浏览器时,它被发送 "as is" 所以在这种情况下 <boolean>
元素被视为 HTML 所以只有包含的文本 true
输出。
要解决此问题,您首先需要在将 ResponseText
发送到浏览器之前对 HTML 进行编码,以便浏览器知道将发送的内容视为纯旧文本。您可以通过调用方法 Server.HTMLEncode()
Response.Write "ok." & Server.HTMLEncode(obj1.ResponseText)
根据MSDN;
The HTMLEncode method applies HTML encoding to a specified string. This is useful as a quick method of encoding form data and other client request data before using it in your Web application. Encoding data converts potentially unsafe characters to their HTML-encoded equivalent.
If the string to be encoded is not Double-Byte Character Set (DBCS), HTMLEncode converts characters as follows:
- The less-than character (
<
) is converted to<
.- The greater-than character (
>
) is converted to>
.- The ampersand character (
&
) is converted to&
.- The double-quote character (
"
) is converted to"
.- Any ASCII code character whose code is greater-than or equal to
0x80
is converted to&#<number>
, where<number>
is the ASCII character value.If the string to be encoded is DBCS, HTMLEncode converts characters as follows:
- All extended characters are converted.
- Any ASCII code character whose code is greater-than or equal to
0x80
is converted to&#<number>
, where<number>
is the ASCII character value.- Half-width Katakana characters in the Japanese code page are not converted.
在发送 ResponseText
的那一刻发生了这种情况;
响应文本
<boolean xmlns="http://somewebsite.com/">true</boolean>
客户端输出
ok.true
如果您使用 Server.HTMLEncode()
它将是;
响应文本 (HTML 已编码)
<boolean xmlns="e;http://somewebsite.com/"e;>true</boolean>
客户端输出
ok.<boolean xmlns="http://somewebsite.com/">true</boolean>
所以我想我可能没有理解您的需求。如果要解析 XML 以将其删除,则仅包含;
的内容<boolean xmlns="http://somewebsite.com/">true</boolean>
在这种情况下 返回给浏览器 true
那么您需要使用 XPath 之类的东西解析 XML 以获取基础值。
'After the initial Send()
Dim xml, root
If obj1.Status = 200 Then
Set xml = obj1.ResponseXML
Call xml.SetProperty("SelectionLanguage", "XPath")
Set root = xml.DocumentElement
Call Response.Write(root.SelectSingleNode("boolean").Text)
End If