SocketsHttpHandler.ConnectCallback Propriété
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient ou définit un rappel personnalisé utilisé pour ouvrir de nouvelles connexions.
public:
property Func<System::Net::Http::SocketsHttpConnectionContext ^, System::Threading::CancellationToken, System::Threading::Tasks::ValueTask<System::IO::Stream ^>> ^ ConnectCallback { Func<System::Net::Http::SocketsHttpConnectionContext ^, System::Threading::CancellationToken, System::Threading::Tasks::ValueTask<System::IO::Stream ^>> ^ get(); void set(Func<System::Net::Http::SocketsHttpConnectionContext ^, System::Threading::CancellationToken, System::Threading::Tasks::ValueTask<System::IO::Stream ^>> ^ value); };
public Func<System.Net.Http.SocketsHttpConnectionContext,System.Threading.CancellationToken,System.Threading.Tasks.ValueTask<System.IO.Stream>>? ConnectCallback { get; set; }
member this.ConnectCallback : Func<System.Net.Http.SocketsHttpConnectionContext, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<System.IO.Stream>> with get, set
Public Property ConnectCallback As Func(Of SocketsHttpConnectionContext, CancellationToken, ValueTask(Of Stream))
Méthode de rappel pour créer un flux.
L’exemple de code suivant définit TCP KeepAlive
sur le sous-jacent Socket
.
static async Task Main()
{
using SocketsHttpHandler handler = new SocketsHttpHandler();
handler.ConnectCallback = async (ctx, ct) =>
{
var s = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
try
{
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, 5);
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, 5);
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 5);
await s.ConnectAsync(ctx.DnsEndPoint, ct);
return new NetworkStream(s, ownsSocket: true);
}
catch
{
s.Dispose();
throw;
}
};
// Create an HttpClient object
using HttpClient client = new HttpClient(handler);
// Call asynchronous network methods in a try/catch block to handle exceptions
try
{
HttpResponseMessage response = await client.GetAsync("https://docs.microsoft.com/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Read {responseBody.Length} characters");
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine($"Message: {e.Message} ");
}
}
Produit | Versions |
---|---|
.NET | 5, 6, 7, 8, 9 |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :