Bagikan melalui


HttpClientChannel Kelas

Definisi

Menerapkan saluran klien untuk panggilan jarak jauh yang menggunakan protokol HTTP untuk mengirimkan pesan.

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
Warisan
Penerapan

Contoh

Contoh kode berikut menunjukkan cara menggunakan HttpClientChannel untuk menyiapkan server jarak jauh dan kliennya. Contohnya berisi tiga bagian:

  • Server

  • Klien

  • Objek jarak jauh yang digunakan oleh server dan klien

Contoh kode berikut menunjukkan 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.");
    }
}

Contoh kode berikut menunjukkan klien untuk server ini.

#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());
    }
}

Contoh kode berikut menunjukkan objek jarak jauh yang digunakan oleh server dan klien.

#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);
    }
}

Keterangan

Penting

Metode panggilan dari kelas ini dengan data yang tidak tepercaya adalah risiko keamanan. Panggil metode dari kelas ini hanya dengan data tepercaya. Untuk informasi selengkapnya, lihat Memvalidasi Semua Input.

Saluran mengangkut pesan di seluruh batas jarak jauh (misalnya, antara komputer atau domain aplikasi). Kelas ini HttpClientChannel mengangkut pesan menggunakan protokol HTTP.

Saluran digunakan oleh infrastruktur jarak jauh .NET Framework untuk mengangkut panggilan jarak jauh. Ketika klien melakukan panggilan ke objek jarak jauh, panggilan diserialisasikan ke dalam pesan yang dikirim oleh saluran klien dan diterima oleh saluran server. Kemudian dideserialisasi dan diproses. Setiap nilai yang dikembalikan ditransmisikan oleh saluran server dan diterima oleh saluran klien.

Untuk melakukan pemrosesan pesan tambahan di sisi klien, Anda dapat menentukan implementasi IClientChannelSinkProvider di mana semua pesan yang diproses oleh HttpClientChannel diteruskan.

Secara default, HttpServerChannel menggunakan pemformat SOAP untuk membuat serial semua pesan.

Objek HttpClientChannel memiliki properti konfigurasi terkait yang dapat diatur pada durasi baik dalam file konfigurasi (dengan memanggil metode statis RemotingConfiguration.Configure ) atau secara terprogram (dengan meneruskan IDictionary koleksi ke HttpClientChannel konstruktor). Untuk daftar properti konfigurasi ini, lihat Properti Konfigurasi Saluran dan Formatter.

Konstruktor

HttpClientChannel()

Menginisialisasi instans baru kelas HttpClientChannel.

HttpClientChannel(IDictionary, IClientChannelSinkProvider)

Menginisialisasi instans HttpClientChannel baru kelas dengan properti konfigurasi dan sink yang ditentukan.

HttpClientChannel(String, IClientChannelSinkProvider)

Menginisialisasi instans HttpClientChannel baru kelas dengan nama dan sink yang ditentukan.

Bidang

SinksWithProperties

Menunjukkan sink saluran atas di tumpukan sink saluran.

(Diperoleh dari BaseChannelWithProperties)

Properti

ChannelName

Mendapatkan nama saluran saat ini.

ChannelPriority

Mendapatkan prioritas saluran saat ini.

Count

Mendapatkan jumlah properti yang terkait dengan objek saluran.

(Diperoleh dari BaseChannelObjectWithProperties)
IsFixedSize

Mendapatkan nilai yang menunjukkan apakah jumlah properti yang dapat dimasukkan ke objek saluran diperbaiki.

(Diperoleh dari BaseChannelObjectWithProperties)
IsReadOnly

Mendapatkan nilai yang menunjukkan apakah kumpulan properti di objek saluran bersifat baca-saja.

(Diperoleh dari BaseChannelObjectWithProperties)
IsSecured

Mendapatkan atau mengatur apakah saluran klien diamankan.

IsSynchronized

Mendapatkan nilai yang menunjukkan apakah kamus properti objek saluran disinkronkan.

(Diperoleh dari BaseChannelObjectWithProperties)
Item[Object]

Mengembalikan properti saluran yang ditentukan.

Keys

ICollection Mendapatkan kunci yang terkait dengan properti saluran.

Properties

Mendapatkan properti saluran yang IDictionary terkait dengan objek saluran saat ini.

(Diperoleh dari BaseChannelWithProperties)
SyncRoot

Mendapatkan objek yang digunakan untuk menyinkronkan akses ke BaseChannelObjectWithProperties.

(Diperoleh dari BaseChannelObjectWithProperties)
Values

ICollection Mendapatkan nilai properti yang terkait dengan objek saluran.

(Diperoleh dari BaseChannelObjectWithProperties)

Metode

Add(Object, Object)

NotSupportedExceptionMelempar .

(Diperoleh dari BaseChannelObjectWithProperties)
Clear()

NotSupportedExceptionMelempar .

(Diperoleh dari BaseChannelObjectWithProperties)
Contains(Object)

Mengembalikan nilai yang menunjukkan apakah objek saluran berisi properti yang terkait dengan kunci yang ditentukan.

(Diperoleh dari BaseChannelObjectWithProperties)
CopyTo(Array, Int32)

NotSupportedExceptionMelempar .

(Diperoleh dari BaseChannelObjectWithProperties)
CreateMessageSink(String, Object, String)

Mengembalikan sink pesan saluran yang mengirimkan pesan ke URL atau objek data saluran yang ditentukan.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetEnumerator()

Mengembalikan yang IDictionaryEnumerator menghitung semua properti yang terkait dengan objek saluran.

(Diperoleh dari BaseChannelObjectWithProperties)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan dari instans Type saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
Parse(String, String)

Mengekstrak URI saluran dan URI objek terkenal jarak jauh dari URL yang ditentukan.

Remove(Object)

NotSupportedExceptionMelempar .

(Diperoleh dari BaseChannelObjectWithProperties)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Implementasi Antarmuka Eksplisit

IEnumerable.GetEnumerator()

Mengembalikan yang IEnumerator menghitung semua properti yang terkait dengan objek saluran.

(Diperoleh dari BaseChannelObjectWithProperties)

Metode Ekstensi

Cast<TResult>(IEnumerable)

Mentransmisikan elemen dari ke IEnumerable jenis yang ditentukan.

OfType<TResult>(IEnumerable)

Memfilter elemen berdasarkan IEnumerable jenis yang ditentukan.

AsParallel(IEnumerable)

Mengaktifkan paralelisasi kueri.

AsQueryable(IEnumerable)

Mengonversi menjadi IEnumerableIQueryable.

Berlaku untuk