c# - 从经典 ASP 将 HTML 解析为 Web 服务

标签 c# .net asp-classic

我正在尝试构建一个网络服务,用户可以在其中输入 HTML,然后通过网络服务验证它是否是有效的 HTML。

我的示例(仅基本)是:

Set objXML = CreateObject("Msxml2.ServerXMLHTTP.6.0")         
Dim strEnvelope 
strEnvelope = "input=" & Server.HTMLEncode("<b>") 
Call objXML.Open("POST", "http://[DOMAIN]/WebServices/HTMLWebService.asmx/ValidateHTML", false) 
Call objXML.SetRequestHeader("Content-Type","application/x-www-form-urlencoded") 
Call objXML.Send(strEnvelope) 

Response.Write(strEnvelope & "##" & objXML.responseText & "##")

我的 ASPX 页面验证输入,然后返回是否为真或假(在字符串数组 XML 中)。目前它总是返回错误。 经过一些调查,似乎由于某种原因,ASMX 收到的输入是空的,这与 HTML 标记有关——要么它不是 HTML 编码的,要么不被 asmx 脚本接受。它返回以下内容:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<string />
<string>false</string>
</ArrayOfString>

是我将输入直接放回返回的 xml 字符串数组的地方。如果我将“”硬编码到该值中,xml 会将其返回为“”,这正是我所期望的。

如果我只是输入“b”然后它返回 false 但确实在字符串数组的第一项中给我一个值“b” - 所以它肯定会拾取该值。

有没有人曾经将 html 解析为 ASP.NET (c#) web 服务或有任何建议?

最佳答案

不能 100% 确定,但由于您在请求 header 中指定内容类型为 "application/x-www-form-urlencoded",这可能是您需要使用 URLEncode 而不是 HTMLEncode。 因此,尝试使用:

strEnvelope = "input=" & Server.URLEncode("<b>") 

关于c# - 从经典 ASP 将 HTML 解析为 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13013649/

相关文章:

string - 将 UTF-8 字符串转换为 ISO-8859-1

c#.NET USB 设备持久标识符

c# - 为什么我得到 'Operator ' !此结构实现中的 =' can not be applied to operands of type Point and <null>'

C# 这个方法线程安全吗?

c# - 在 .Net 中编译为 native 代码是否会完全删除 MSIL?

c# - StringBuilder 解决了什么问题?

c# - MVC OutputCaching 是否优先于设置缓存响应 header ?

asp-classic - FileSystemObject 是否知道文件不完整?

asp.net - SQL Server 登录前握手确认错误

c# - 如何返回一个已取消的 ValueTask<T> 并传播一个 OperationCanceledException,而不需要 async/await?