0

*Form1_Load* でアプリの最新バージョンを取得するために、このコードを使用しています。

string result1 = null;
string url1 = "http://site.com/version.html";
WebResponse response1 = null;
StreamReader reader1 = null;

try
{
   HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url1);
   request1.Method = "GET";
   response1 = request1.GetResponse();
   reader1 = new StreamReader(response1.GetResponseStream(), Encoding.UTF8);
   result1 = reader1.ReadToEnd();
 }
 catch (Exception ex)
 {
   // show the error if any.                
 }
 finally
 {
    if (reader1 != null)
         reader1.Close();
    if (response1 != null)
         response1.Close();
 }

問題は、サーバーをシャットダウンすると、アプリケーション全体が動かなくなり、次のようなウィンドウが表示されることです。

リモートサーバーに接続できません

これは合法のようです。

このクラッシュ (サーバーがダウンしている場合) を回避し、バージョン チェックを回避する方法はありますか?

4

1 に答える 1

1

表示されている特定の例外タイプをキャッチする追加の catch ブロックを追加します...コードは次のようになります...

try
{
//*yadda yadda yadda*
}
catch (System.Net.WebException WebEx)
{
//*Correctly set up a situation where the rest of your program will know there was a connection problem to the website.*
}
catch (Exception ex)
{
//*Do the error catching you do now*
}
finally
{
//*yadda yadda*
}

この構造により、他の種類の例外とは異なる方法で WebExceptions を処理できます。すべての例外は 1 つの基本クラス Exception から派生し、このような用途のために独自のものを作成できることに注意してください。

于 2012-08-15T20:08:42.107 に答える