HttpClientChannel Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Implementuje klientský kanál pro vzdálená volání, která k přenosu zpráv používá protokol 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
- Dědičnost
- Implementuje
Příklady
Následující příklad kódu ukazuje, jak použít HttpClientChannel k nastavení vzdálené komunikace serveru a jeho klienta. Příklad obsahuje tři části:
Server
Klient
Vzdálený objekt používaný serverem a klientem
Následující příklad kódu ukazuje server.
#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.");
}
}
Následující příklad kódu ukazuje klienta pro tento server.
#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());
}
}
Následující příklad kódu ukazuje vzdálený objekt používaný serverem a klientem.
#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);
}
}
Poznámky
Důležité
Volání metod z této třídy s nedůvěryhodnými daty představuje bezpečnostní riziko. Volejte metody z této třídy pouze s důvěryhodnými daty. Další informace naleznete v tématu Ověření všech vstupů.
Kanály přenášejí zprávy přes hranice vzdálené komunikace (například mezi počítači nebo doménami aplikace). Třída HttpClientChannel přenáší zprávy pomocí protokolu HTTP.
Kanály používají infrastruktura vzdálené komunikace .NET Framework k přenosu vzdálených volání. Když klient provede volání vzdáleného objektu, volání se serializuje do zprávy, která je odeslána klientským kanálem a přijata kanálem serveru. Pak se deserializuje a zpracuje. Všechny vrácené hodnoty jsou přenášeny kanálem serveru a přijaty klientským kanálem.
Chcete-li provést další zpracování zpráv na straně klienta, můžete zadat implementaciIClientChannelSinkProvider, prostřednictvím které jsou předány všechny zprávy zpracovávané.HttpClientChannel
Ve výchozím nastavení HttpServerChannel se k serializaci všech zpráv používá formátovací modul SOAP.
Objekt HttpClientChannel má přidružené vlastnosti konfigurace, které lze nastavit za běhu buď v konfiguračním souboru (vyvoláním statické RemotingConfiguration.Configure metody), nebo programově (předáním IDictionary kolekce konstruktoru HttpClientChannel ).
Konstruktory
| Name | Description |
|---|---|
| HttpClientChannel() |
Inicializuje novou instanci HttpClientChannel třídy. |
| HttpClientChannel(IDictionary, IClientChannelSinkProvider) |
Inicializuje novou instanci HttpClientChannel třídy se zadanými vlastnostmi konfigurace a jímkou. |
| HttpClientChannel(String, IClientChannelSinkProvider) |
Inicializuje novou instanci HttpClientChannel třídy se zadaným názvem a jímkou. |
Pole
| Name | Description |
|---|---|
| SinksWithProperties |
Označuje horní kanál jímku v zásobníku jímky kanálu. (Zděděno od BaseChannelWithProperties) |
Vlastnosti
| Name | Description |
|---|---|
| ChannelName |
Získá název aktuálního kanálu. |
| ChannelPriority |
Získá prioritu aktuálního kanálu. |
| Count |
Získá počet vlastností přidružených k objektu kanálu. (Zděděno od BaseChannelObjectWithProperties) |
| IsFixedSize |
Získá hodnotu, která určuje, zda počet vlastností, které lze zadat do objektu kanálu je pevné. (Zděděno od BaseChannelObjectWithProperties) |
| IsReadOnly |
Získá hodnotu, která určuje, zda kolekce vlastností v objektu kanálu je jen pro čtení. (Zděděno od BaseChannelObjectWithProperties) |
| IsSecured |
Získá nebo nastaví, zda je klientský kanál zabezpečený. |
| IsSynchronized |
Získá hodnotu, která určuje, zda slovník vlastností objektu kanálu je synchronizován. (Zděděno od BaseChannelObjectWithProperties) |
| Item[Object] |
Vrátí zadanou vlastnost kanálu. |
| Keys |
ICollection Získá klíče, ke kterým jsou vlastnosti kanálu přidruženy. |
| Properties |
IDictionary Získá vlastnosti kanálu přidružené k aktuálnímu objektu kanálu. (Zděděno od BaseChannelWithProperties) |
| SyncRoot |
Získá objekt, který se používá k synchronizaci přístupu k objektu BaseChannelObjectWithProperties. (Zděděno od BaseChannelObjectWithProperties) |
| Values |
ICollection Získá hodnoty vlastností přidružených k objektu kanálu. (Zděděno od BaseChannelObjectWithProperties) |
Metody
| Name | Description |
|---|---|
| Add(Object, Object) |
Vyhodí .NotSupportedException (Zděděno od BaseChannelObjectWithProperties) |
| Clear() |
Vyhodí .NotSupportedException (Zděděno od BaseChannelObjectWithProperties) |
| Contains(Object) |
Vrátí hodnotu, která určuje, zda objekt kanálu obsahuje vlastnost, která je přidružena k zadanému klíči. (Zděděno od BaseChannelObjectWithProperties) |
| CopyTo(Array, Int32) |
Vyhodí .NotSupportedException (Zděděno od BaseChannelObjectWithProperties) |
| CreateMessageSink(String, Object, String) |
Vrátí jímku zpráv kanálu, která doručuje zprávy do zadané adresy URL nebo datového objektu kanálu. |
| Equals(Object) |
Určí, zda se zadaný objekt rovná aktuálnímu objektu. (Zděděno od Object) |
| GetEnumerator() |
IDictionaryEnumerator Vrátí výčet všech vlastností přidružených k objektu kanálu. (Zděděno od BaseChannelObjectWithProperties) |
| GetHashCode() |
Slouží jako výchozí funkce hash. (Zděděno od Object) |
| GetType() |
Získá Type aktuální instance. (Zděděno od Object) |
| MemberwiseClone() |
Vytvoří mělkou kopii aktuálního Object. (Zděděno od Object) |
| Parse(String, String) |
Extrahuje identifikátor URI kanálu a vzdálený dobře známý identifikátor URI objektu ze zadané adresy URL. |
| Remove(Object) |
Vyhodí .NotSupportedException (Zděděno od BaseChannelObjectWithProperties) |
| ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |
Explicitní implementace rozhraní
| Name | Description |
|---|---|
| IEnumerable.GetEnumerator() |
IEnumerator Vrátí výčet všech vlastností přidružených k objektu kanálu. (Zděděno od BaseChannelObjectWithProperties) |
Metody rozšíření
| Name | Description |
|---|---|
| AsParallel(IEnumerable) |
Umožňuje paralelizaci dotazu. |
| AsQueryable(IEnumerable) |
Převede IEnumerable na IQueryable. |
| Cast<TResult>(IEnumerable) |
Přetypuje prvky IEnumerable na zadaný typ. |
| OfType<TResult>(IEnumerable) |
Filtruje prvky IEnumerable na základě zadaného typu. |