Share via


HttpClientChannel 類別

定義

針對遠端呼叫實作使用 HTTP 通訊協定傳輸訊息的用戶端通道。

public ref class HttpClientChannel : System::Runtime::Remoting::Channels::BaseChannelWithProperties, System::Runtime::Remoting::Channels::IChannelSender
public ref class HttpClientChannel : System::Runtime::Remoting::Channels::BaseChannelWithProperties, System::Runtime::Remoting::Channels::IChannelSender, System::Runtime::Remoting::Channels::ISecurableChannel
public class HttpClientChannel : System.Runtime.Remoting.Channels.BaseChannelWithProperties, System.Runtime.Remoting.Channels.IChannelSender
public class HttpClientChannel : System.Runtime.Remoting.Channels.BaseChannelWithProperties, System.Runtime.Remoting.Channels.IChannelSender, System.Runtime.Remoting.Channels.ISecurableChannel
type HttpClientChannel = class
    inherit BaseChannelWithProperties
    interface IChannelSender
    interface IChannel
type HttpClientChannel = class
    inherit BaseChannelWithProperties
    interface IChannelSender
    interface IChannel
    interface ISecurableChannel
Public Class HttpClientChannel
Inherits BaseChannelWithProperties
Implements IChannelSender
Public Class HttpClientChannel
Inherits BaseChannelWithProperties
Implements IChannelSender, ISecurableChannel
繼承
實作

範例

下列程式碼範例示範如何使用 HttpClientChannel 來設定遠端伺服器及其用戶端。 此範例包含三個部分:

  • 伺服器

  • 用戶端

  • 伺服器和用戶端所使用的遠端物件

下列程式碼範例顯示伺服器。

#using <System.dll>
#using <System.Runtime.Remoting.dll>
#using "common.dll"
using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Http;

void main()
{
   // Create the server channel.
   HttpServerChannel^ serverChannel = gcnew HttpServerChannel( 9090 );
   
   // Register the server channel.
   ChannelServices::RegisterChannel( serverChannel );
   
   // Expose an object for remote calls.
   RemotingConfiguration::RegisterWellKnownServiceType( RemoteObject::typeid, L"RemoteObject.rem", WellKnownObjectMode::Singleton );
   
   // Wait for the user prompt.
   Console::WriteLine( L"Press ENTER to exit the server." );
   Console::ReadLine();
   Console::WriteLine( L"The server is exiting." );
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

public class Server
{
    public static void Main(string[] args)
    {
        // Create the server channel.
        HttpServerChannel serverChannel = new HttpServerChannel(9090);

        // Register the server channel.
        ChannelServices.RegisterChannel(serverChannel);

        // Expose an object for remote calls.
        RemotingConfiguration.RegisterWellKnownServiceType(
            typeof(RemoteObject), "RemoteObject.rem",
            WellKnownObjectMode.Singleton);

        // Wait for the user prompt.
        Console.WriteLine("Press ENTER to exit the server.");
        Console.ReadLine();
        Console.WriteLine("The server is exiting.");
    }
}

下列程式碼範例顯示此伺服器的用戶端。

#using <System.dll>
#using <System.Runtime.Remoting.dll>
#using "common.dll"

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Http;
void main()
{
   // Create the channel.
   HttpClientChannel^ clientChannel = gcnew HttpClientChannel;

   // Register the channel.
   ChannelServices::RegisterChannel( clientChannel );

   // Register as client for remote object.
   WellKnownClientTypeEntry^ remoteType = gcnew WellKnownClientTypeEntry( RemoteObject::typeid,L"http://localhost:9090/RemoteObject.rem" );
   RemotingConfiguration::RegisterWellKnownClientType( remoteType );

   // Create a message sink.
   String^ objectUri;
   System::Runtime::Remoting::Messaging::IMessageSink^ messageSink = clientChannel->CreateMessageSink( L"http://localhost:9090/RemoteObject.rem", nullptr,  objectUri );
   Console::WriteLine( L"The URI of the message sink is {0}.", objectUri );
   if ( messageSink != nullptr )
   {
      Console::WriteLine( L"The type of the message sink is {0}.", messageSink->GetType() );
   }

   // Display the channel's properties using Keys and Item.
   for each(String^ key in clientChannel->Keys)
   {
       Console::WriteLine("clientChannel[{0}] = <{1}>", key, clientChannel[key]);
   }

   // Parse the channel's URI.
   String^ objectUrl = L"http://localhost:9090/RemoteObject.rem";
   String^ channelUri = clientChannel->Parse( objectUrl,  objectUri );
   Console::WriteLine( L"The object URL is {0}.", objectUrl );
   Console::WriteLine( L"The object URI is {0}.", objectUri );
   Console::WriteLine( L"The channel URI is {0}.", channelUri );

   // Create an instance of the remote object.
   RemoteObject^ service = gcnew RemoteObject;
   
   // Invoke a method on the remote object.
   Console::WriteLine( L"The client is invoking the remote object." );
   Console::WriteLine( L"The remote object has been called {0} times.", service->GetCount() );
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

public class Client
{
    public static void Main(string[] args)
    {
        // Create the channel.
        HttpClientChannel clientChannel = new HttpClientChannel();

        // Register the channel.
        ChannelServices.RegisterChannel(clientChannel);

        // Register as client for remote object.
        WellKnownClientTypeEntry remoteType =
            new WellKnownClientTypeEntry(typeof(RemoteObject),
            "http://localhost:9090/RemoteObject.rem");
        RemotingConfiguration.RegisterWellKnownClientType(remoteType);

        // Create a message sink.
        string objectUri;
        System.Runtime.Remoting.Messaging.IMessageSink messageSink =
            clientChannel.CreateMessageSink(
            "http://localhost:9090/RemoteObject.rem",
            null, out objectUri);
        Console.WriteLine(
            "The URI of the message sink is {0}.",
            objectUri);
        if (messageSink != null)
        {
            Console.WriteLine("The type of the message sink is {0}.",
                messageSink.GetType().ToString());
        }

        // Display the channel's properties using Keys and Item.
        foreach(string key in clientChannel.Keys)
        {
            Console.WriteLine(
                "clientChannel[{0}] = <{1}>",
                key, clientChannel[key]);
        }

        // Parse the channel's URI.
        string objectUrl = "http://localhost:9090/RemoteObject.rem";
        string channelUri = clientChannel.Parse(objectUrl, out objectUri);
        Console.WriteLine("The object URL is {0}.", objectUrl);
        Console.WriteLine("The object URI is {0}.", objectUri);
        Console.WriteLine("The channel URI is {0}.", channelUri);

        // Create an instance of the remote object.
        RemoteObject service = new RemoteObject();

        // Invoke a method on the remote object.
        Console.WriteLine("The client is invoking the remote object.");
        Console.WriteLine("The remote object has been called {0} times.",
            service.GetCount());
    }
}

下列程式碼範例顯示伺服器和用戶端所使用的遠端物件。

#using <System.dll>
using namespace System;
using namespace System::Runtime::Remoting;

// Remote object.
public ref class RemoteObject: public MarshalByRefObject
{
private:
   static int callCount = 0;

public:
   int GetCount()
   {
      Console::WriteLine( L"GetCount was called." );
      callCount++;
      return (callCount);
   }

};
using System;
using System.Runtime.Remoting;

// Remote object.
public class RemoteObject : MarshalByRefObject
{
    private int callCount = 0;

    public int GetCount()
    {
        Console.WriteLine("GetCount was called.");
        callCount++;
        return(callCount);
    }
}

備註

重要

使用不信任的資料呼叫此類別的方法,會造成安全性上的風險。 呼叫此類別的方法時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

通道會跨遠端界限傳輸訊息 (例如電腦或應用程式域之間) 。 類別 HttpClientChannel 會使用 HTTP 通訊協定傳輸訊息。

.NET Framework遠端基礎結構會使用通道來傳輸遠端呼叫。 當用戶端呼叫遠端物件時,呼叫會序列化為用戶端通道所傳送且由伺服器通道接收的訊息。 然後,它會還原序列化和處理。 任何傳回的值都是由伺服器通道傳輸,並由用戶端通道接收。

若要在用戶端執行其他訊息處理,您可以指定 的實作,透過此實 IClientChannelSinkProvider 作傳遞 的所有 HttpClientChannel 訊息。

根據預設,會 HttpServerChannel 使用 SOAP 格式器來序列化所有訊息。

HttpClientChannel物件具有相關聯的組態屬性,可在組態檔中設定 (,方法是叫用靜態 RemotingConfiguration.Configure 方法) ,或藉由將集合傳遞 IDictionaryHttpClientChannel 建構函式) ,以程式設計方式 (。 如需這些組態屬性的清單,請參閱 通道和格式器組態屬性

建構函式

HttpClientChannel()

初始化 HttpClientChannel 類別的新執行個體。

HttpClientChannel(IDictionary, IClientChannelSinkProvider)

使用指定的組態屬性和接收,初始化 HttpClientChannel 類別的新執行個體。

HttpClientChannel(String, IClientChannelSinkProvider)

使用指定的名稱和接收,初始化 HttpClientChannel 類別的新執行個體。

欄位

SinksWithProperties

指示通道接收堆疊中的頂端通道接收。

(繼承來源 BaseChannelWithProperties)

屬性

ChannelName

取得目前通道的名稱。

ChannelPriority

取得目前通道的優先權。

Count

取得與通道物件相關聯的屬性數目。

(繼承來源 BaseChannelObjectWithProperties)
IsFixedSize

取得值,指出可輸入到通道物件的屬性數目是否為固定的。

(繼承來源 BaseChannelObjectWithProperties)
IsReadOnly

取得值,指出通道物件中的屬性集合是否為唯讀。

(繼承來源 BaseChannelObjectWithProperties)
IsSecured

取得或設定用戶端通道是否受到保護。

IsSynchronized

取得值,指出通道物件屬性的字典是否已經同步 (Synchronize)。

(繼承來源 BaseChannelObjectWithProperties)
Item[Object]

傳回指定的通道屬性。

Keys

取得與通道屬性相關聯之索引鍵的 ICollection

Properties

取得與目前通道物件相關聯之通道屬性的 IDictionary

(繼承來源 BaseChannelWithProperties)
SyncRoot

取得可用來對 BaseChannelObjectWithProperties 同步存取的物件。

(繼承來源 BaseChannelObjectWithProperties)
Values

取得與通道物件相關聯之屬性值的 ICollection

(繼承來源 BaseChannelObjectWithProperties)

方法

Add(Object, Object)

擲回 NotSupportedException

(繼承來源 BaseChannelObjectWithProperties)
Clear()

擲回 NotSupportedException

(繼承來源 BaseChannelObjectWithProperties)
Contains(Object)

傳回值,指出通道物件是否包含與指定之索引鍵相關聯的屬性。

(繼承來源 BaseChannelObjectWithProperties)
CopyTo(Array, Int32)

擲回 NotSupportedException

(繼承來源 BaseChannelObjectWithProperties)
CreateMessageSink(String, Object, String)

傳回通道訊息接收,其傳遞訊息給指定 URL 或通道資料物件。

Equals(Object)

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

(繼承來源 Object)
GetEnumerator()

傳回 IDictionaryEnumerator,其列舉與通道物件相關聯的所有屬性。

(繼承來源 BaseChannelObjectWithProperties)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Parse(String, String)

從指定的 URL 擷取通道 URI 和遠端已知物件 URI。

Remove(Object)

擲回 NotSupportedException

(繼承來源 BaseChannelObjectWithProperties)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IEnumerable.GetEnumerator()

傳回 IEnumerator,其列舉與通道物件相關聯的所有屬性。

(繼承來源 BaseChannelObjectWithProperties)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於