Question

I need to check whether a given host is replying to HTTP web requests. So, all I need to do is HttpWebRequest.Create(someURI) and check the response, right?

HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(targetUri);

TimeSpan timeOut = TimeSpan.FromSeconds(timeOutInSeconds);
webRequest.Timeout = (int)timeOut.TotalMilliseconds;
webRequest.Proxy = proxy;
webRequest.AllowAutoRedirect = false; // get extra information with this turned off, but doesn't affect the problem

response = webRequest.GetResponse() as HttpWebResponse;

Well, the problem that's come up is that if someURI is blocked by our firewall (e.g. "gibber.com" is blocked), then I get back a valid HttpWebResponse, it doesn't throw, and the ResponseURI property is set to the blocked URI ("gibber.com") even though it's actually our server responding.

Within the HTTP header there will be a Location key and its value is the IP of the local server. But I don't want to parse text to work out if a request actually bounced. What's the correct way to do this?

Thanks

Was it helpful?

Solution

check to see if response.ResponseUri corresponds to the given host or your firewall.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top