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