Skip to content

Commit 0d1dc88

Browse files
committed
Resolve IP addresses in ConnectCallback example
1 parent 1788789 commit 0d1dc88

File tree

1 file changed

+6
-2
lines changed
  • snippets/csharp/System.Net.Http/SocketsHttpHandler/ConnectCallback

1 file changed

+6
-2
lines changed

snippets/csharp/System.Net.Http/SocketsHttpHandler/ConnectCallback/program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Net;
44
using System.Net.Http;
55
using System.Net.Sockets;
6+
using System.Threading;
67
using System.Threading.Tasks;
78

89
class HttpClientHandler_SecureExample
@@ -14,14 +15,17 @@ static async Task Main()
1415

1516
handler.ConnectCallback = async (ctx, ct) =>
1617
{
18+
DnsEndPoint dnsEndPoint = ctx.DnsEndPoint;
19+
IPAddress[] addresses = await Dns.GetHostAddressesAsync(dnsEndPoint.Host, dnsEndPoint.AddressFamily, ct);
1720
var s = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
1821
try
1922
{
2023
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
2124
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, 5);
2225
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, 5);
2326
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 5);
24-
await s.ConnectAsync(ctx.DnsEndPoint, ct);
27+
28+
await s.ConnectAsync(addresses, dnsEndPoint.Port, ct);
2529
return new NetworkStream(s, ownsSocket: true);
2630
}
2731
catch

0 commit comments

Comments
 (0)