0

ユーザー提供の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( System.Net.Browser.ClientHttpWebRequest。<>c__DisplayClassa.b__8(Object state2)at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)atSystem.のNetwork_Monitor.Checks.URLCheckResults.RespCallback(IAsyncResult非同期結果)のIAsyncResult asyncResult) System.Threading.ThreadPool.WorkItem.doWork(Object o)atSystem.Threading.TimerのThreading.ExecutionContext.Run(ExecutionContextexecutionContext、ContextCallbackコールバック、オブジェクト状態)。指輪()

コールスタック:

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に表示されるが、他のWebサイトには表示されない原因は何ですか。

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))、次のスクリーンショットの行で発生しています(右クリックして画像を表示をクリックすると拡大表示されます)。

例外画像

4

1 に答える 1

0

ME:yahoo.com、google.com、msn.comを使用して完全に実行されました。私のコードとあなたのコードの唯一の違いは、ディスパッチャーの外でイテレーションを実行したことです。あなたが試したときにグーグルがひっかかっていた可能性はありますか?

ガブリエル:何と言ったらいいのかわからない。それを答えとして作成してください。+100の評判をお返しします。

于 2012-08-14T20:42:30.357 に答える