I’m trying to get headers from a user provided url and all seems to be working fine until I try http://www.google.com which gives me the following exception:
System.NullReferenceException was unhandled Message=NotSupportedException
StackTrace:
at System.Net.Browser.OHWRAsyncResult.get_AsyncWaitHandle()
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at Network_Monitor.Checks.URLCheckResults.RespCallback(IAsyncResult asynchronousResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.b__8(Object state2)
at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadPool.WorkItem.doWork(Object o)
at System.Threading.Timer.ring()
Call Stack:
System.Windows.dll!System.Net.Browser.AsyncHelper.BeginOnUI(System.Threading.SendOrPostCallback beginMethod, object state) + 0xc3 bytes
System.Windows.dll!System.Net.Browser.ClientHttpWebRequest.EndGetResponse(System.IAsyncResult asyncResult) + 0x41 bytes
Network Monitor.dll!Network_Monitor.Checks.URLCheckResults.RespCallback(System.IAsyncResult asynchronousResult) Line 55 + 0x3 bytes C#
System.Windows.dll!System.Net.Browser.ClientHttpWebRequest.InvokeGetResponseCallback.AnonymousMethod__8(object state2) + 0x1b bytes
mscorlib.dll!System.Threading.ThreadPool.WorkItem.WaitCallback_Context(object state) + 0x18 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x63 bytes
mscorlib.dll!System.Threading.ThreadPool.WorkItem.doWork(object o) + 0x47 bytes
mscorlib.dll!System.Threading.Timer.ring() + 0x70 bytes
I’ve Googled for hours looking for the solution but none of the existing ones work. What is cause this to appear for Google.com but not other websites?
public void CheckURL(String URL)
{
lblResults.Text = "Checking...";
var wr = HttpWebRequest.Create(URL);
wr.Method = "HEAD";
IAsyncResult asyncResult = (IAsyncResult)wr.BeginGetResponse(RespCallback, wr);
}
private void RespCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
{
Dispatcher.BeginInvoke(() =>
{
for (int i = 0; i < response.Headers.Count; ++i)
{
if (!lblResults.Text.Equals("Checking..."))
{
lblResults.Text += "\n";
}
else
{
lblResults.Text = "";
}
lblResults.Text += "Header Name:" + response.Headers.AllKeys[i] + ", Header value :" + response.Headers[response.Headers.AllKeys[i]];
}
});
}
}
catch (WebException ex)
{
if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
{
Dispatcher.BeginInvoke(() =>
{
lblResults.Text = "Page Not Found (404)";
});
}
else
{
MessageBox.Show(ex.Message, "Program Name", MessageBoxButton.OK);
}
}
}
The exception is happening on the line using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult)) with the following screenshot (Right Click and click view image to see it bigger):

1 Answer