I had a way to send GET requests, for example:
private JArray GetRESTData(string uri)
{
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string s = reader.ReadToEnd();
return JsonConvert.DeserializeObject<JArray>(s);
}
catch
{
try
{
MessageBox.Show(GetScalarVal(uri));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return null;
}
... I changed it to handle POST requests by inserting this between the webRequest and webResponse assignments:
if (uri.ToUpper().Contains("POST"))
{
webRequest.Method = "POST";
webRequest.ContentLength = 0;
}
... and renamed it GetOrPostRESTData ()
But this violates the principle of shared responsibility.
However, if I do this in two ways, the POST method is similar to the GET method, except for the additional two lines of code that are otherwise in the conditional ("if Post"), I violate DRY, since most of the code is the same.
Is there a third way? The middle way? Or should I choose between these two violations? I am stuck between a dry and hard place.