0

我试图从用户提供的网址,以获取标题和一切似乎直到我尝试http://www.google.com这给了我下面的异常是工作的罚款:的NullReferenceException是未处理HttpWebResponse

System.NullReferenceException了未处理消息= NotSupportedException异常 堆栈跟踪: 在System.Net.Browser.OHWRAsyncResult.get_AsyncWaitHandle() 在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态) 在System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult的asyncResult)在Network_Monitor.Checks.URLCh处使用 eckResults.RespCallback(IAsyncResult asynchronousResult) at 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(对象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了寻找解决方案小时,但没有一个现有的一些工作。这是什么原因导致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))发生与下面的截图(点击右键,点击查看的图片看到它更大的):

Exception Image

+0

通过标题,你是指在html中的

标记的实际内容?如果是这样,进入谷歌主页并在源代码中搜索单词标题不会显示任何结果,这可能解释NullReferenceException? – 2012-07-24 16:38:03

+0

不,HttpWebResponse标题(我使用“HEAD”的原因) – 2012-07-24 16:50:44

+0

@LukeWyatt我不知道该说些什么。创建一个答案作为答案,我会奖励你100点的声望。 – 2012-08-14 20:36:38

回答

0

ME:这只是跑了我完美使用yahoo.com,google.com和 msn.com。我和你的代码之间的唯一区别是我在Dispatcher之外运行了 迭代。当你尝试过时,谷歌有什么机会打嗝 ?

Gabriel:我不知道该说什么。创建一个答案,我会 奖励你100点的声望。

相关问题