HttpClientChannel 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
實作一個用戶端通道用於遠端通話,使用 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
- 繼承
- 實作
範例
以下程式碼範例說明如何使用 a 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 框架遠端基礎設施用於傳輸遠端通話。 當用戶端呼叫遠端物件時,該呼叫會序列化成由用戶端通道傳送並由伺服器通道接收的訊息。 接著被解序並處理。 任何回傳的值由伺服器通道傳送,並由用戶端通道接收。
若要在用戶端執行額外訊息處理,你可以指定一個實作 IClientChannelSinkProvider ,讓所有由 處理的 HttpClientChannel 訊息都經過此處傳遞。
預設情況下,它 HttpServerChannel 使用 SOAP 格式化器來序列化所有訊息。
物件HttpClientChannel具有相關的組態屬性,可在執行時設定,無論是在設定檔中(透過呼叫靜態RemotingConfiguration.Configure方法)或程式化(將集合傳給IDictionaryHttpClientChannel建構器)中設定。
建構函式
| 名稱 | Description |
|---|---|
| HttpClientChannel() |
初始化 HttpClientChannel 類別的新執行個體。 |
| HttpClientChannel(IDictionary, IClientChannelSinkProvider) |
初始化一個新的類別實例 HttpClientChannel ,並具備指定的組態屬性與 sink。 |
| HttpClientChannel(String, IClientChannelSinkProvider) |
初始化一個以指定名稱和 sink 的新 HttpClientChannel 類別實例。 |
欄位
| 名稱 | Description |
|---|---|
| SinksWithProperties |
表示槽槽匯堆疊中的頂端通道匯。 (繼承來源 BaseChannelWithProperties) |
屬性
| 名稱 | Description |
|---|---|
| ChannelName |
會得到目前頻道的名稱。 |
| ChannelPriority |
獲得當前頻道的優先權。 |
| Count |
取得與通道物件相關的屬性數量。 (繼承來源 BaseChannelObjectWithProperties) |
| IsFixedSize |
會得到一個值,表示通道物件中可輸入屬性數量是否固定。 (繼承來源 BaseChannelObjectWithProperties) |
| IsReadOnly |
會獲得一個值,表示通道物件中的屬性集合是否為唯讀。 (繼承來源 BaseChannelObjectWithProperties) |
| IsSecured |
取得或設定用戶端通道是否安全。 |
| IsSynchronized |
會得到一個值,表示通道物件屬性字典是否同步。 (繼承來源 BaseChannelObjectWithProperties) |
| Item[Object] |
回傳指定的通道特性。 |
| Keys |
取得 ICollection 通道屬性所關聯的一鍵。 |
| Properties |
取得 IDictionary 與目前通道物件相關的通道屬性。 (繼承來源 BaseChannelWithProperties) |
| SyncRoot |
取得一個物件用來同步存取。BaseChannelObjectWithProperties (繼承來源 BaseChannelObjectWithProperties) |
| Values |
取得 ICollection 與通道物件相關屬性的數值。 (繼承來源 BaseChannelObjectWithProperties) |
方法
| 名稱 | Description |
|---|---|
| Add(Object, Object) | (繼承來源 BaseChannelObjectWithProperties) |
| Clear() | (繼承來源 BaseChannelObjectWithProperties) |
| Contains(Object) |
回傳一個值,指示通道物件是否包含與指定鍵相關聯的屬性。 (繼承來源 BaseChannelObjectWithProperties) |
| CopyTo(Array, Int32) | (繼承來源 BaseChannelObjectWithProperties) |
| CreateMessageSink(String, Object, String) |
回傳一個通道訊息匯入器,將訊息傳送到指定的 URL 或通道資料物件。 |
| Equals(Object) |
判斷指定的 物件是否等於目前的物件。 (繼承來源 Object) |
| GetEnumerator() |
回傳 a IDictionaryEnumerator ,列舉所有與通道物件相關的屬性。 (繼承來源 BaseChannelObjectWithProperties) |
| GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Parse(String, String) |
從指定的 URL 中擷取通道 URI 及遠端知名物件 URI。 |
| Remove(Object) | (繼承來源 BaseChannelObjectWithProperties) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| IEnumerable.GetEnumerator() |
回傳 a IEnumerator 枚舉所有與通道物件相關聯的屬性。 (繼承來源 BaseChannelObjectWithProperties) |
擴充方法
| 名稱 | Description |
|---|---|
| AsParallel(IEnumerable) |
啟用查詢的平行處理。 |
| AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
| Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
| OfType<TResult>(IEnumerable) |
根據指定的型別篩選 IEnumerable 的專案。 |