DiscoveryClientProtocol 類別

定義

提供支援,以程式設計方式叫用 (Invoke) XML Web Service 探索 (Discovery)。

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 Form,示範如何使用 類別搭配命名空間中的其他 System.Web.Services.Discovery 類別,以程式設計方式叫用 DiscoveryClientProtocol XML Web 服務探索。 此程式碼範例示範如何使用 DiscoverDiscoverAnyDiscoverResolveAllResolveOneLevelReadAllWriteAll 方法。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ 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 方法的 Discover URL 來啟動 XML Web 服務探索。 一般而言,此 URL 是指探索檔,而此檔又是指描述一或多個 XML Web 服務的檔,這些服務會新增至 References 屬性。 此時,只會下載該檔並加以驗證,以指向 XML Web 服務的有效資訊。 不過,該檔中包含的參考在此階段不會驗證。 相反地,它們會新增至 References 屬性。 若要確認參考有效,請叫 ResolveAll 用 或 ResolveOneLevel 方法,以將有效的參考檔新增至 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 Service 用戶端驗證 (Authentication) 的安全認證。

(繼承來源 WebClientProtocol)
DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
Documents

取得探索文件的集合。

EnableDecompression

取得或設定值,指出是否已啟用這個 HttpWebClientProtocol 的解壓縮。

(繼承來源 HttpWebClientProtocol)
Errors

取得來自這個類別的方法引動過程期間所發生的例外狀況 (Exception) 集合。

Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
PreAuthenticate

取得或設定是否已啟用預先驗證。

(繼承來源 WebClientProtocol)
Proxy

取得或設定 Proxy 資訊,以製作穿越防火牆的 XML Web Service 要求。

(繼承來源 HttpWebClientProtocol)
References

參考的集合會在已解析的探索文件中找到。

RequestEncoding

Encoding,用來建立對 XML Web Service 的用戶端要求。

(繼承來源 WebClientProtocol)
Site

取得或設定 ComponentISite

(繼承來源 Component)
Timeout

表示 XML Web Service 用戶端等待同步 XML Web Service 要求的回覆到達的時間 (單位為毫秒)。

(繼承來源 WebClientProtocol)
UnsafeAuthenticatedConnectionSharing

取得或設定值,指出是否在用戶端使用 NTLM 驗證連接到裝載 XML Web Service 的 Web 伺服器時啟用連線共用。

(繼承來源 HttpWebClientProtocol)
Url

取得或設定用戶端正在要求之 XML Web Service 的基礎 URL。

(繼承來源 WebClientProtocol)
UseDefaultCredentials

取得或設定值,指出是否將 Credentials 屬性設為 DefaultCredentials 屬性的值。

(繼承來源 WebClientProtocol)
UserAgent

針對隨著每個要求所傳送的使用者代理標頭,取得或設定值。

(繼承來源 HttpWebClientProtocol)

方法

Abort()

取消對 XML Web Service 方法的要求。

(繼承來源 WebClientProtocol)
CancelAsync(Object)

取消對 XML Web Service 方法的非同步呼叫,除非呼叫已完成。

(繼承來源 HttpWebClientProtocol)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Discover(String)

探索提供的 URL 以判斷它是否是探索文件。

DiscoverAny(String)

探索提供的 URL 以判斷它是否為探索文件、服務描述或 XML 結構描述定義 (XSD) 結構描述。

Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Component)
Download(String)

下載位於提供的 URL 之探索文件至 Stream 物件。

Download(String, String)

下載位於提供的 URL 之探索文件至 Stream 物件,設定 contentType 參數為探索文件的 MIME 編碼。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetWebRequest(Uri)

建立指定 URL 的 WebRequest

(繼承來源 HttpWebClientProtocol)
GetWebResponse(WebRequest)

從對 XML Web Service 方法的同步要求傳回回應。

(繼承來源 HttpWebClientProtocol)
GetWebResponse(WebRequest, IAsyncResult)

從對 XML Web Service 方法的非同步要求傳回回應。

(繼承來源 HttpWebClientProtocol)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
LoadExternals()
已淘汰.

指示 DiscoveryClientProtocol 物件載入任何外部參考。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ReadAll(String)

讀取包含已儲存探索文件對應的檔案,此文件填入 DocumentsReferences 屬性,並且在檔案中具有所參考的探索文件、XML 結構描述定義 (XSD) 結構描述和服務描述。

ResolveAll()

解析 References 屬性中對探索文件、XML 結構描述定義 (XSD) 結構描述和服務描述的所有參考,以及在參考的探索文件中找到的參考。

ResolveOneLevel()

解析 References 中對探索文件、XML 結構描述定義 (XSD) 結構描述和服務描述的所有參考,以及在那些探索文件中找到的參考。

ToString()

傳回任何包含 Component 名稱的 String。 不應覆寫此方法。

(繼承來源 Component)
WriteAll(String, String)

將所有在 Documents 屬性中的探索文件、XML 結構描述定義 (XSD) 結構描述和服務描述寫入提供的目錄,並在該目錄中建立檔案。

事件

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)

適用於

另請參閱