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

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

默认情况下, HttpServerChannel 使用 SOAP 格式化程序来序列化所有消息。

对象HttpClientChannel具有关联的配置属性,可以通过调用静态RemotingConfiguration.Configure方法) 或通过将集合HttpClientChannel传递给IDictionary构造函数) 以编程方式 (在配置文件 (中设置这些属性。 有关这些配置属性的列表,请参阅 Channel 和 Formatter 配置属性

构造函数

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)

扩展方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于