0

ちょっとかさばるようなコードがあります。ステートメントを 2 つ持つ代わりに、ステートメントを含むステートメントをtry1 つだけ持つ方法はありifますか?

 private bool Method1()
{
   _responseValue = null;
   {
      if (_httpProxy != null)
      {
         //start edit here
         try
         {
            HttpWebRequest _get = _Url;
            _get.Method = "GET";

            WebProxy _proxy = _httpProxy;
            _get.Proxy = _proxy;

            _responseValue = (HttpWebResponse)_get.GetResponse();
            Console.WriteLine("Good"));
            return true;
         }
         catch (WebException e)
         {
            Console.WriteLine("Exception"));
            if (e.Status == WebExceptionStatus.ProtocolError) _responseValue = (HttpWebResponse)e.Response;
            else return false;
         }
         catch (Exception)
         {
            if (_responseValue != null) _responseValue.Close();
            return false;
         }
      }
      else
      {
         try
         {
            HttpWebRequest _get = _Url;
            _get.Method = "GET";

            _responseValue = (HttpWebResponse)_get.GetResponse();
            Console.WriteLine("Good"));
            return true;
         }
         catch (WebException x)
         {
            Console.WriteLine("Exception"));
            if (x.Status == WebExceptionStatus.ProtocolError) _responseValue = (HttpWebResponse)x.Response;
            else return false;
         }
         catch (Exception)
         {
            if (_responseValue != null) _responseValue.Close();
            return false;
         }
      }
      return true;
      //end edit here
   }
}
4

2 に答える 2