如何:创建用于分析网页内容的 Web 服务

本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端: Windows Communication Foundation.

使用 ASP.NET 创建的 Web 服务提供了 HTML 分析解决方案,使开发人员可以分析远程 HTML 页的内容并以编程方式公开所得到的数据。有关详细说明,请参见 ASP.NET XML Web services 进行的 HTML 分析

指定操作和输入参数

  1. 创建一个 Web 服务描述语言 (WSDL) 文档,该文档通常以文件扩展名 .wsdl 来保存。根据 WSDL 架构,该文档的内容必须由有效的 XML 构成。对于原型,您可以使用为在 ASP.NET 上运行的 Web 服务动态生成的 WSDL 文档。通过在 Web 服务 URL 中追加 ?wsdl 参数来发出请求。

  2. 指定用于为分析 HTML 文本的每个 Web 服务方法定义操作的元素。此步骤及下一步骤要求熟悉 WSDL 格式。

  3. 如果分析方法采用输入参数,则需指定表示这些参数的元素并将它们与操作关联。

指定从分析的 HTML 页返回的数据

  1. 在通过 XPath /definitions/binding/operation/output 显示的 <output> 元素内添加一个命名空间限定的 <text> XML 元素。<operation> 元素表示检索已分析的 HTML 的 Web 服务方法。
xxb0bsdh.note(zh-cn,VS.100).gif注意:
绑定内的操作名称必须是全局唯一的,或者 Wsdl.exe 可与指定的命名空间一起运行,以防止导入到同一应用程序中的其他 WSDL 文件导致命名冲突。

  1. <text> XML 元素内的服务说明中为要从分析的 HTML 页中返回的所有数据添加 <match> XML 元素。

  2. 将特性应用于 <match> 元素。主题“ASP.NET XML Web services 进行的 HTML 分析”下的表中显示了有效特性。

为 Web 服务生成客户端代理代码

  1. 运行来自 Windows(R) 软件开发工具包 (SDK) 的 Wsdl.exe 工具。将您创建的 WSDL 文件作为输入来传递。

示例

下面的代码示例是一个包含 <TITLE><H1> 标记的简单网页示例。

<HTML>
 <HEAD>
  <TITLE>Sample Title</TITLE>
 </HEAD>
 <BODY>
    <H1>Some Heading Text</H1>
 </BODY>
</HTML>

下面的代码示例是一个服务说明,它对 HTML 页的内容进行分析,从而提取 <TITLE><H1> 标记内的文本内容。在该代码示例中,针对 GetTitleHttpGet 绑定定义 TestHeaders 方法。TestHeaders 方法在 <match> XML 元素中定义了两组可从分析的 HTML 页返回的数据:TitleH1,它们分别分析 <TITLE><H1> 标记的内容。

<?xml version="1.0"?>
<definitions xmlns:s="http://www.w3.org/2001/XMLSchema"
             xmlns:http="https://schemas.xmlsoap.org/wsdl/http/"
             xmlns:mime="https://schemas.xmlsoap.org/wsdl/mime/"
             xmlns:soapenc="https://schemas.xmlsoap.org/soap/encoding/"
             xmlns:soap="https://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:s0="http://tempuri.org/"
             targetNamespace="http://tempuri.org/"
             xmlns="https://schemas.xmlsoap.org/wsdl/">
  <types>
    <s:schema targetNamespace="http://tempuri.org/"
              attributeFormDefault="qualified"
              elementFormDefault="qualified">
      <s:element name="TestHeaders">
        <s:complexType derivedBy="restriction"/>
      </s:element>
      <s:element name="TestHeadersResult">
        <s:complexType derivedBy="restriction">
          <s:all>
            <s:element name="result" type="s:string" nullable="true"/>
          </s:all>
        </s:complexType>
      </s:element>
      <s:element name="string" type="s:string" nullable="true"/>
    </s:schema>
  </types>
  <message name="TestHeadersHttpGetIn"/>
  <message name="TestHeadersHttpGetOut">
    <part name="Body" element="s0:string"/>
  </message>
  <portType name="GetTitleHttpGet">
    <operation name="TestHeaders">
      <input message="s0:TestHeadersHttpGetIn"/>
      <output message="s0:TestHeadersHttpGetOut"/>
    </operation>
  </portType>
  <binding name="GetTitleHttpGet" type="s0:GetTitleHttpGet">
    <http:binding verb="GET"/>
    <operation name="TestHeaders">
      <http:operation location="MatchServer.html"/>
      <input>
        <http:urlEncoded/>
      </input>
      <output>
         <text xmlns="https://microsoft.com/wsdl/mime/textMatching/">
          <match name='Title' pattern='TITLE&gt;(.*?)&lt;'/>
          <match name='H1' pattern='H1&gt;(.*?)&lt;'/>
         </text>
      </output>
    </operation>
  </binding>
  <service name="GetTitle">
    <port name="GetTitleHttpGet" binding="s0:GetTitleHttpGet">
      <http:address location="https://localhost" />
    </port>
  </service>
</definitions>

下面的代码示例是 Wsdl.exe 为前面的服务说明生成的代理类的一部分。

' GetTitle is the name of the proxy class.
Public Class GetTitle
  Inherits HttpGetClientProtocol
  Public Function TestHeaders() As TestHeadersMatches
     Return CType(Me.Invoke("TestHeaders", (Me.Url + _
          "/MatchServer.html"), New Object(-1) {}),TestHeadersMatches)
  End Function
End Class
Public Class TestHeadersMatches    Public Title As String    Public H1 As String
End Class
' GetTitle is the name of the proxy class.
public class GetTitle : HttpGetClientProtocol
{
  public TestHeadersMatches TestHeaders() 
  {
        return ((TestHeadersMatches)(this.Invoke("TestHeaders", 
                 (this.Url + "/MatchServer.html"), new object[0])));
  }
}    
public class TestHeadersMatches 
{
    public string Title;    public string H1;
}

另请参见

参考

Web 服务描述语言工具 (Wsdl.exe)
MatchAttribute

概念

ASP.NET XML Web services 进行的 HTML 分析

其他资源

.NET Framework Regular Expressions
使用 ASP.NET 的 XML Web services