通过


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
继承
实现

示例

下面的代码示例演示如何使用 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框架远程处理基础结构使用通道来传输远程呼叫。 当客户端调用远程对象时,调用将序列化为客户端通道发送并由服务器通道接收的消息。 然后对其进行反序列化和处理。 任何返回的值都由服务器通道传输,并由客户端通道接收。

若要在客户端执行其他消息处理,可以指定传递所有消息的HttpClientChannel实现IClientChannelSinkProvider

默认情况下,使用 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

获取一个值,该值指示通道对象属性的字典是否同步。

(继承自 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)

扩展方法

名称 说明
AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

适用于