我正在尝试从用户提供的网址获取标头,并且所有内容似乎都正常工作,直到我尝试http://www.google.com
,这给了我以下异常:
System.NullReferenceException未处理Message = NotSupportedException 堆栈跟踪: 在System.Net.Browser.OHWRAsyncResult.get_AsyncWaitHandle() 在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state) 在System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在Network_Monitor.Checks.URLCheckResults.RespCallback(IAsyncResult asynchronousResult) 在System.Net.Browser.ClientHttpWebRequest。<> c__DisplayClassa.b__8(对象state2) 在System.Threading.ThreadPool.WorkItem.WaitCallback_Context(对象状态) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态) 在System.Threading.ThreadPool.WorkItem.doWork(Object o) 在System.Threading.Timer.ring()
调用堆栈:
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))
行,并带有以下屏幕截图(右键单击并单击查看图像以查看更大):
答案 0 :(得分:0)
我:它完全使用yahoo.com,google.com和 msn.com。我和你的代码之间的唯一区别是我跑了 Dispatcher之外的迭代。谷歌有任何机会打嗝 当你试过?
加布里埃尔:我不知道该说些什么。创建它作为答案,我会 奖励你+100声望。