DiscoveryClientProtocol 类

定义

为以编程方式调用 XML Web services 发现提供支持。

public ref class DiscoveryClientProtocol : System::Web::Services::Protocols::HttpWebClientProtocol
public class DiscoveryClientProtocol : System.Web.Services.Protocols.HttpWebClientProtocol
type DiscoveryClientProtocol = class
    inherit HttpWebClientProtocol
Public Class DiscoveryClientProtocol
Inherits HttpWebClientProtocol
继承

示例

下面的代码示例是一个 Web 窗体,演示如何将 DiscoveryClientProtocol 类与 命名空间中的其他 System.Web.Services.Discovery 类一起使用,以编程方式调用 XML Web 服务发现。 代码示例演示如何使用 Discover、、DiscoverAnyDiscoverResolveAllResolveOneLevelReadAllWriteAll 方法。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Page Language="C#" Debug="true" %>

<%@ Import Namespace="System.Web.Services.Discovery" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Data" %>

<HTML>
<HEAD>
   <SCRIPT RUNAT="SERVER">
   protected void Discover_Click(object Source, EventArgs e)
   {
    // Specify the URL to discover.
    string sourceUrl = DiscoURL.Text;
    // Specify the URL to save discovery results to or read from.
    string outputDirectory = DiscoDir.Text;

        DiscoveryClientProtocol client = new DiscoveryClientProtocol();
    // Use default credentials to access the URL being discovered.
        client.Credentials = CredentialCache.DefaultCredentials;

        try {
          DiscoveryDocument doc;
      // Check to see if whether the user wanted to read in existing discovery results.
      if (DiscoverMode.Value == "ReadAll") 
          {
         DiscoveryClientResultCollection results = client.ReadAll(Path.Combine(DiscoDir.Text,"results.discomap"));
            SaveMode.Value = "NoSave";						
      }
      else 
          {
        // Check to see if whether the user wants the capability to discover any kind of discoverable document.
        if (DiscoverMode.Value == "DiscoverAny") 
            {
          doc = client.DiscoverAny(sourceUrl);
            }
        else
        // Discover only discovery documents, which might contain references to other types of discoverable documents.
            {
          doc = client.Discover(sourceUrl);
        }
        // Check to see whether the user wants to resolve all possible references from the supplied URL.
        if (ResolveMode.Value == "ResolveAll")
           client.ResolveAll();
        else 
            {
        // Check to see whether the user wants to resolve references nested more than one level deep.
            if (ResolveMode.Value == "ResolveOneLevel")  
               client.ResolveOneLevel();
            else
           Status.Text = String.Empty;
            }
          }
        }
        catch ( Exception e2) 
        {
          DiscoveryResultsGrid.Columns.Clear();
          Status.Text = e2.Message;
        }
    // If documents were discovered, display the results in a data grid.
        if (client.Documents.Count > 0)
        PopulateGrid(client);

    // If the user also asked to have the results saved to the Web server, do so.
        if (SaveMode.Value == "Save") 
        {
          DiscoveryClientResultCollection results = client.WriteAll(outputDirectory, "results.discomap");
      Status.Text = "The following file holds the links to each of the discovery results: <b>" + 
                                    Path.Combine(outputDirectory,"results.discomap") + "</b>";
        }
                             
     
      }

      protected void PopulateGrid(DiscoveryClientProtocol client) 
      {
         DataTable dt = new DataTable();
         DataRow dr;
 
         dt.Columns.Add(new DataColumn("Discovery Document"));
         dt.Columns.Add(new DataColumn("References"));
         dt.Columns.Add(new DataColumn("Type"));


         foreach (DictionaryEntry entry in client.Documents) 
         {
                dr = dt.NewRow();
        dr[0] = (string) entry.Key;
        dr[2] = entry.Value.GetType();
        dt.Rows.Add(dr);
        if (entry.Value is DiscoveryDocument)
        {
          DiscoveryDocument discoDoc = (DiscoveryDocument) entry.Value;
          foreach (DiscoveryReference discoref in discoDoc.References)
          {
            dr = dt.NewRow();
            dr[1] = discoref.Url;
            dr[2] = discoref.GetType();
            dt.Rows.Add(dr);
           }
        }
        
         }
        DataView dv = new DataView(dt);
    DiscoveryResultsGrid.DataSource = (ICollection) dv;
    DiscoveryResultsGrid.DataBind();
      
    }
  </SCRIPT>
  </HEAD> 
  <BODY>
    <H3> <p align="center"> Discovery Class Sample </p> </H3>
        <FORM RUNAT="SERVER">
    <hr>	
     Enter the URL to discover:
        <asp:textbox id=DiscoURL Columns=60 runat="SERVER" /><p>

       Discovery Mode:
       <select id="DiscoverMode" size=1 runat="SERVER">
         <option Value="DiscoverAny">Discover any of the discovery types</option>
             <option Value="Discover">Discover just discovery documents</option>
             <option Value="ReadAll">Read in saved discovery results</option>
    </select> <p>

       Resolve References Mode:
       <select id="ResolveMode" size=1 runat="SERVER">
             <option Value="ResolveAll">Resolve all references</option>
             <option Value="ResolveOneLevel">Resolve references only in discovery documents within the supplied URL</option>
             <option Value="ResolveNone">Do not resolve references</option>
    </select> <p>
        
       Save Results Mode:
    <select id="SaveMode" size=1 runat="SERVER">
         <option Value="NoSave">Do not save any of the discovery documents found locally</option>
             <option Value="Save">Save the discovery documents found locally</option>
        </select> <p>
        Enter the directory to Read/Save the Discovery results:
        <asp:textbox id=DiscoDir runat="SERVER" /> <p>

    <p align="center"> <asp:Button id=Discover Text="Discover!" onClick="Discover_Click" runat="SERVER"/> </p><p>

        <hr>
        <asp:label id="Status" runat="SERVER" /><p>
     <asp:DataGrid id="DiscoveryResultsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="true"
           runat="server">

         <HeaderStyle BackColor="DarkBlue" ForeColor="White">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="LightYellow">
         </AlternatingItemStyle>

     </asp:DataGrid>
        </FORM>
  </BODY>
<%@ Page Language="VB" Debug="true" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web.Services.Discovery" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Data" %>

<HTML>
<HEAD>
   <SCRIPT RUNAT="SERVER">
   Public Sub Discover_Click(Source As Object, e as EventArgs )
      ' Specify the URL to discover.
      Dim sourceUrl as String = DiscoURL.Text
      ' Specify the URL to save discovery results to or read from.
      Dim outputDirectory As String = DiscoDir.Text

      Dim client as DiscoveryClientProtocol = new DiscoveryClientProtocol()
      ' Use default credentials to access the URL being discovered.
      client.Credentials = CredentialCache.DefaultCredentials
      Try 
        Dim doc As DiscoveryDocument
        ' Check to see whether the user wanted to read in existing discovery results.
    If (DiscoverMode.Value = "ReadAll") Then
       Dim results As DiscoveryClientResultCollection 
           results = client.ReadAll(Path.Combine(DiscoDir.Text,"results.discomap"))
       SaveMode.Value = "NoSave"						
    Else
       ' Check to see whether the user user wants the capability to discover any kind of discoverable document.
           If (DiscoverMode.Value = "DiscoverAny") Then
         doc = client.DiscoverAny(sourceUrl)
           Else
         ' Discover only discovery documents, which might contain references to other types of discoverable documents. 
         doc = client.Discover(sourceUrl)
       End If
           
           ' Check to see whether the user wants to resolve all possible references from the supplied URL.
       If (ResolveMode.Value = "ResolveAll") Then
          client.ResolveAll()
           ' Check to see whether the user wants to resolve references nested more than one level deep.
       ElseIf (ResolveMode.Value = "ResolveOneLevel")  Then
              client.ResolveOneLevel()
       Else
          Status.Text = String.Empty
           End If
    End If
            
       Catch e2 As Exception
          DiscoveryResultsGrid.Columns.Clear()
          Status.Text = e2.Message
       End Try

       ' If documents were discovered, display the results in a data grid.
       If (client.Documents.Count > 0) Then
            'populate our Grid with the discovery results.
        PopulateGrid(client)
       End If

       ' If the user also asked to have the results saved to the Web server, do so.	    
       If (SaveMode.Value = "Save") Then
      Dim results As DiscoveryClientResultCollection 
          results = client.WriteAll(outputDirectory, "results.discomap")
          Status.Text = "The following file holds the links to each of the discovery results: <b>" + _ 
                                     Path.Combine(outputDirectory,"results.discomap") + "</b>"
       End If                             

      End Sub
      Public Sub PopulateGrid(client As DiscoveryClientProtocol) 
         Dim dt As DataTable = new DataTable()
         Dim dr AS DataRow 
 
         dt.Columns.Add(new DataColumn("Discovery Document") )
         dt.Columns.Add(new DataColumn("References") )
         dt.Columns.Add(new DataColumn("Type") )

     Dim entry As DictionaryEntry
         For Each entry in client.Documents
            dr = dt.NewRow()
        dr(0) = entry.Key
        dr(2) = entry.Value.GetType()
        dt.Rows.Add(dr)
        If TypeOf entry.Value Is DiscoveryDocument Then
           Dim discoDoc As DiscoveryDocument = entry.Value
           Dim discoref As DiscoveryReference
           For Each discoref in discoDoc.References
          dr = dt.NewRow()
          dr(1) = discoref.Url
          dr(2) = discoref.GetType()
          dt.Rows.Add(dr)
           Next
        End If   
    Next 	
         
        Dim dv As DataView = new DataView(dt)
    DiscoveryResultsGrid.DataSource = dv
    DiscoveryResultsGrid.DataBind()
     End Sub
  </SCRIPT>
  </HEAD> 
  <BODY>
    <H3> <p align="center"> Discovery Class Sample </p> </H3>
        <FORM RUNAT="SERVER">

    <hr>	
        Enter the URL to discover:
        <asp:textbox id=DiscoURL Columns=60 runat="SERVER" /><p>

       Discovery Mode:
       <select id="DiscoverMode" size=1 runat="SERVER">
         <option Value="DiscoverAny">Discover any of the discovery types</option>
             <option Value="Discover">Discover just discovery documents</option>
             <option Value="ReadAll">Read in saved discovery results</option>
    </select> <p>

       Resolve References Mode:
       <select id="ResolveMode" size=1 runat="SERVER">
          <option Value="ResolveAll">Resolve all references</option>
             <option Value="ResolveOneLevel">Resolve references only in discovery documents within the supplied URL</option>
             <option Value="ResolveNone">Do not resolve references</option>
    </select> <p>
        
       Save Results Mode:
    <select id="SaveMode" size=1 runat="SERVER">
          <option Value="NoSave">Do not save any of the discovery documents found locally</option>
             <option Value="Save">Save the discovery documents found locally</option>
        </select> <p>
        Enter the directory to Read/Save the Discovery results:
        <asp:textbox id=DiscoDir runat="SERVER" /> <p>


    <p align="center"> <asp:Button id=Discover Text="Discover!" onClick="Discover_Click" runat="SERVER"/> </p><p>

        <hr>
        <asp:label id="Status" runat="SERVER" /><p>
     <asp:DataGrid id="DiscoveryResultsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="true"
           runat="server">

         <HeaderStyle BackColor="DarkBlue" ForeColor="White">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="LightYellow">
         </AlternatingItemStyle>

     </asp:DataGrid>
        </FORM>
  </BODY>

注解

XML Web 服务发现是查找或发现描述可用 XML Web 服务的一个或多个相关文档的过程。 XML Web 服务客户端通过 XML Web 服务发现了解给定 URL 处的可用 XML Web 服务及其使用方式。 XML Web 服务发现基于已获取发现文档的 URL(可能通过目录服务)的前提进行,但是,你没有关于所提供的 XML Web 服务的详细信息。 通过 XML Web 服务发现,可以发现特定 URL 处的 中列出的 DiscoveryDocument XML Web 服务的详细信息。

XML Web 服务客户端通过向 或 DiscoverAny 方法提供 URL Discover 来启动 XML Web 服务发现。 通常,此 URL 引用发现文档,而发现文档又引用描述添加到 属性的一个或多个 XML Web 服务 References 的文档。 此时,仅下载并验证该文档以指向有关 XML Web 服务的有效信息。 但是,此阶段不会验证该文档中包含的引用。 而是将其添加到 References 属性。 若要验证引用是否有效,请调用 ResolveAllResolveOneLevel 方法,这些方法将有效的引用文档添加到 Documents 属性。 最后,如果客户端想要将发现结果保存到磁盘,请调用 WriteAll 方法。

如果不需要以编程方式访问 XML Web 服务发现,Windows SDK 会提供 Web 服务发现工具 (Disco.exe) ,用于在命令提示符内发现 XML Web 服务。 有关详细信息,请参阅 Web 服务发现工具 (Disco.exe)

构造函数

DiscoveryClientProtocol()

初始化 DiscoveryClientProtocol 类的新实例。

属性

AdditionalInformation

获取除在发现文档中找到的引用以外的信息。

AllowAutoRedirect

获取或设置客户端是否自动跟随服务器重定向。

(继承自 HttpWebClientProtocol)
CanRaiseEvents

获取一个指示组件是否可以引发事件的值。

(继承自 Component)
ClientCertificates

获取客户证书集合。

(继承自 HttpWebClientProtocol)
ConnectionGroupName

获取或设置请求的连接组的名称。

(继承自 WebClientProtocol)
Container

获取包含 IContainerComponent

(继承自 Component)
CookieContainer

获取或设置 Cookie 集合。

(继承自 HttpWebClientProtocol)
Credentials

获取或设置 XML Web services 客户端身份验证的安全凭据。

(继承自 WebClientProtocol)
DesignMode

获取一个值,用以指示 Component 当前是否处于设计模式。

(继承自 Component)
Documents

获取发现文档的集合。

EnableDecompression

获取或设置一个值,该值指示是否为此 HttpWebClientProtocol 启用压缩。

(继承自 HttpWebClientProtocol)
Errors

获取在调用此类方法的过程中发生的异常的集合。

Events

获取附加到此 Component 的事件处理程序的列表。

(继承自 Component)
PreAuthenticate

获取或设置是否启用了预身份验证。

(继承自 WebClientProtocol)
Proxy

获取或设置用于通过防火墙进行 XML Web services 请求的代理信息。

(继承自 HttpWebClientProtocol)
References

在已解析的发现文档中找到的引用的集合。

RequestEncoding

用于对 XML Web services 发出客户端请求的 Encoding

(继承自 WebClientProtocol)
Site

获取或设置 ComponentISite

(继承自 Component)
Timeout

指示 XML Web services 客户端等待同步 XML Web services 请求完成的时间(以毫秒计)的回复。

(继承自 WebClientProtocol)
UnsafeAuthenticatedConnectionSharing

获取或设置一个值,它指示客户端使用 NTLM 身份验证连接到承载 XML Web services 的 Web 服务器时,是否启用连接共享。

(继承自 HttpWebClientProtocol)
Url

获取或设置客户端正在请求的 XML Web services 的基 URL。

(继承自 WebClientProtocol)
UseDefaultCredentials

获取或设置一个值,该值指示是否将 Credentials 属性设置为 DefaultCredentials 属性的值。

(继承自 WebClientProtocol)
UserAgent

获取或设置随每个请求发送的用户代理标头的值。

(继承自 HttpWebClientProtocol)

方法

Abort()

取消对 XML Web services 方法的请求。

(继承自 WebClientProtocol)
CancelAsync(Object)

取消对 XML Web services 方法的异步调用,除非已完成该调用。

(继承自 HttpWebClientProtocol)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(继承自 MarshalByRefObject)
Discover(String)

发现提供的 URL 以确定它是否为发现文档。

DiscoverAny(String)

发现提供的 URL 以确定它是否是发现文档、服务说明或 XML 架构定义 (XSD) 架构。

Dispose()

释放由 Component 使用的所有资源。

(继承自 Component)
Dispose(Boolean)

释放由 Component 占用的非托管资源,还可以另外再释放托管资源。

(继承自 Component)
Download(String)

将所提供的 URL 处的发现文档下载到 Stream 对象中。

Download(String, String)

将位于提供的 URL 处的发现文档下载到 Stream 对象中,并将 contentType 参数设置为发现文档的 MIME 编码。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetService(Type)

返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。

(继承自 Component)
GetType()

获取当前实例的 Type

(继承自 Object)
GetWebRequest(Uri)

为指定的 URI 创建一个 WebRequest

(继承自 HttpWebClientProtocol)
GetWebResponse(WebRequest)

将同步请求中的响应返回给 XML Web services 方法。

(继承自 HttpWebClientProtocol)
GetWebResponse(WebRequest, IAsyncResult)

将异步请求中的响应返回给 XML Web services 方法。

(继承自 HttpWebClientProtocol)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
LoadExternals()
已过时.

指示 DiscoveryClientProtocol 对象加载任意外部引用。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
ReadAll(String)

读入包含已保存的发现文档的映射的文件,并使用该文件中引用的发现文档、XML 架构定义 (XSD) 架构和服务说明填充 DocumentsReferences 属性。

ResolveAll()

解析所有对 References 属性中的发现文档、XML 架构定义 (XSD) 架构和服务说明的引用以及在引用的发现文档中找到的引用。

ResolveOneLevel()

解析 References 中所有对发现文档、XML 架构定义 (XSD) 架构以及服务说明的引用以及在那些发现文档中找到的引用。

ToString()

返回包含 Component 的名称的 String(如果有)。 不应重写此方法。

(继承自 Component)
WriteAll(String, String)

Documents 属性中的所有发现文档、XML 架构定义 (XSD) 架构和服务说明写入提供的目录并在此目录中创建一个文件。

事件

Disposed

在通过调用 Dispose() 方法释放组件时发生。

(继承自 Component)

适用于

另请参阅