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