我正在尝试从用户提供的 url 获取标头,并且在我尝试之前一切似乎都工作正常,http://www.google.com
这给了我以下异常:
System.NullReferenceException 未处理 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) 在 System.Net.Browser.ClientHttpWebRequest 的 Network_Monitor.Checks.URLCheckResults.RespCallback(IAsyncResult asynchronousResult)。<>c__DisplayClassa.b__8(Object state2) 在 System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state) 在 System。 Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadPool.WorkItem.doWork(Object o) 在 System.Threading.Timer。戒指()
调用堆栈:
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
我已经用谷歌搜索了几个小时来寻找解决方案,但现有的解决方案都不起作用。是什么导致它出现在 Google.com 而不是其他网站?
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);
}
}
}
异常发生在using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
以下屏幕截图(右键单击并单击查看图像以查看大图):