Context
I have a laptop in a corporate network using a proxy.
When I use the HTTP client (.NET Framework 4.8) in an application it works fine.
When I unplug internet, I get The remote name could not be resolved
which makes sense.
However, when reconnecting internet while the app is still running, I keep getting The remote name could not be resolved
even though I can verify it is working from my browser.
Restarting the application makes everything work again.
I have a small reproducible example combining things I found in different blog posts about other issues related to The remote name could not be resolved
.
Question
Is there some caching being done somewhere deep internal? In the example I have new HttpClient
's, new HttpClientHandler
's, set the ConnectionClose
header and put the ServicePointManager
. If there is, how can I manipulate it?
(sidenote: When I'm using this in my applications outside this small reproducible example I'm using the AddHttpClient<TI,T>
where the containing classes are not singleton and have this same result.)
internal class Program
{
static async Task Main(string[] args)
{
var sp = ServicePointManager.FindServicePoint(new Uri("https://www.google.com"));
sp.SetTcpKeepAlive(false,0,0);
sp.ConnectionLeaseTimeout = (int)TimeSpan.FromSeconds(15).TotalMilliseconds;
ServicePointManager.DnsRefreshTimeout = (int)TimeSpan.FromSeconds(15).TotalMilliseconds;
while (true)
{
try
{
using (var client = new HttpClient(new HttpClientHandler()))
{
Console.WriteLine("Press Key to try send");
Console.ReadKey();
var uri = "https://www.google.com";
var message = new HttpRequestMessage(HttpMethod.Get, uri);
message.Headers.ConnectionClose = true;
message.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = TimeSpan.FromSeconds(1),
};
var response = await client.SendAsync(message);
Console.WriteLine("Success:");
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
Update
After reading some tracing thanks to @Lex Li I saw the following.
Request 1: Connected
System.Net Verbose: 0 : [25368] Entering ServicePoint#6044116::ServicePoint(myproxyaddress:8080)
Request 2: Disconnected
System.Net Verbose: 0 : [29900] Entering ServicePoint#3507177::ServicePoint(www.google.com:443)
Request 3: Reconnected
System.Net Information: 0 : [29900] Associating HttpWebRequest#37200859 with ServicePoint#3507177
So it connects to the wrong servicepoint.
When I move the HttpClient
and HttpClientHandler
creation out of the loop, it works.
It however does not work if I start from a disconnected state (due to associating with wrong servicepoint).
I Think I need to explicitly set the proxy settings when applicable instead of letting .NET figure it out and caching it wrongfully.
Update
Indeed, explicitly setting the proxy address worked. This allows for no mistakes when .NET tries to figure out how to connect by itself.
var handler = new HttpClientHandler()
{
Proxy = new WebProxy("myproxyadress:8080")
};
var client = new HttpClient(handler);
CodePudding user response:
Digged through some tracing thanks to suggestion from @Lex Li
Explicitly setting the proxy address worked. This allows for no mistakes when .NET tries to figure out how to connect by itself by associating requests with potentially wrong service endpoints.
var handler = new HttpClientHandler()
{
Proxy = new WebProxy("myproxyadress:8080")
};
var client = new HttpClient(handler);