如何简化我的代码块?

时间:2013-12-06 19:00:44

标签: c# httpwebrequest try-catch code-cleanup webproxy

我有一些看起来有点笨重的代码。有没有办法代替两个try语句,只有一个语句里面有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
   }
}

2 个答案:

答案 0 :(得分:2)

        try
        {
            HttpWebRequest _request = _httpUrl;
            _request.Method = "GET";

            if (_httpProxy != null)
            {
                _request.Proxy = _httpProxy;
                proxymessage = "with Proxy";
            }
            else
                proxymessage = "without Proxy";

            _response = (HttpWebResponse)_request.GetResponse();
            _strBuilderVerbose.Append(String.Format("HttpWebRequest {0} was successful",proxymessage));
            return true;
        }
        catch (WebException e)
        {
            _strBuilderVerbose.Append(String.Format("Catch 'WebException e' {0} was called",proxymessage));
            if (e.Status == WebExceptionStatus.ProtocolError) _response = (HttpWebResponse)e.Response;
            else return false;
        }
        catch (Exception)
        {
            if (_response != null) _response.Close();
            return false;
        }

        return true;
    }

答案 1 :(得分:2)

将此常见逻辑结果分解为此代码

private bool ExecuteCommand()
{
    bool resp ;

    _response = null;

    try
    {
        HttpWebRequest _request = _httpUrl;
        _request.Method = "GET";

        if (_httpProxy != null)
        {       
            _request.Proxy = _httpProxy;
        }

        _response = (HttpWebResponse)_request.GetResponse();
        _strBuilderVerbose.Append(String.Format("HttpWebRequest was successful"));
        resp = true ;
    }
    catch (WebException e)
    {
        _strBuilderVerbose.Append(String.Format("Catch 'WebException e' "));

        if (e.Status == WebExceptionStatus.ProtocolError)
        {               
            _response = (HttpWebResponse)e.Response;
        }
        else 
        {
            resp = false;
        }
    }
    catch (Exception)
    {
        if (_response != null) 
        {
            _response.Close();
        }
        resp =  false;
    }

    return resp ;
}

顺便说一下,避免那些多次退货