HttpWebRequest - Is there anyway to clear IP Address cached in ServicePoint?

  • Thread starter Thread starter Frendy
  • Start date Start date
F

Frendy

Anyone know is there any workaround or is there anyway I can clear the
IP address cached by the Service Point?
I have a C# service that keep submitting to a server using a
HTTPWebrequest. The problem is when the remote server fail-over,
HTTPWebRequest still trying to submit to the old server/IPAddress. It
looks like the ServicePoint cache the IP Address internally by the
hostname.
After fail-over, the hostname is still remain the same but the IP has
changed. Is there anyway I can clear the cache? BTW, I can't submit
using the IP Address because there is SSL that tie with the hostname.

The following is the snippet to reproduce the problem.

1. Create a Windows App. add a button and a textbox with multiline

2. Add the following code into the button onclick event
HttpWebRequest objHttpWebRequest =
(HttpWebRequest)WebRequest.Create(@"http://www.google.com");
objHttpWebRequest.Method = "GET";
objHttpWebRequest.Timeout = 5000;
WebResponse objResponse = objHttpWebRequest.GetResponse();
StreamReader objStreamReader = new
StreamReader(objResponse.GetResponseStream(),
System.Text.Encoding.GetEncoding("utf-8"));
textBox1.Text = objStreamReader.ReadToEnd();
objStreamReader.Close();
objResponse.Close();

3. Click on the button once and you will see google's HTML code in the
textbox

4. To Reproduce the failover, go to your local hosts file
(C:\WINNT\system32\drivers\etc\hosts) and add the following entry so
the traffic will go to dell.com website while the program still
hitting the google url.

143.166.83.230 www.google.com

5. Click the button again and pay attention that the code is still
referencing to google website. But once you close the app. and open
again, then it will see the dell website.

I tried using the ConnectionGroupName, ServicePoint.MaxIdleTime
nothing is working.
I can make it to work in debug by manipulating the local variable
value in Service point:
m_CurrentAddressInfoIndex = m_IPHostEntryInfo.IPAddressInfoList.Length
m_ConnectionsSinceDns = -1

Since ServicePoint is an internal class there is no way I can override
it. Any recommendation will be appreciated.

Thanks
 
Back
Top