I have an environment where DNS name may resolve to different IP address outside my control; also the IP address may change and be blocked by external firewall. I am able to log IP address used for connection this way:
public static IPEndPoint BindIPEndPoint1(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
Console.Write($"IP address = {remoteEndPoint.ToString()}");
return null;
}
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request = (HttpWebRequest)WebRequest.Create(url);
request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPoint1);
request.Proxy = new WebProxy();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
stop.Stop();
if (response.StatusCode == HttpStatusCode.OK)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"OK in {stop.ElapsedMilliseconds} ms");
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{response.StatusCode} in {stop.ElapsedMilliseconds} ms");
}
}
catch (System.Net.WebException exs)
{
stop.Stop();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{exs.Message} in {stop.ElapsedMilliseconds} ms");
}
This shows IP address for successful connections; but if it fails with System.Net.WebException "Timed Out" It does not identify what IP address is used. Is there any other way to identify within the program what IP address the URL resolved to in these failed requests. Doing a DNS lookup is not suitable as it may not match the exact request that generated the failure. In addition accessing the site by address does not work as the site requires accessed by the hostname.