How can I simplify my codeblock?

99 Views Asked by At

I have some code that seems kinda bulky. Is there a way of instead of having two try statements, have only one with if statements inside?

 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

There are 2 best solutions below

1
On BEST ANSWER
        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;
    }
0
On

Factoring the common logic results in this code

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 ;
}

By the way, avoid those multiple returns