[Solved] How to reduce redundant coding in java?


I assume that the isHttps variable check is there for a reason(?) and therefore the second cast should actually be HttpURLConnection, meaning there is a typo in the question?
If so the most methods used in the question are available in the parent class URLConnection without a cast, but not all.

Fortunately HttpsURLConnection is a subclas of HttpUrlConnection so just always casting to that will work here, replace all with (no need for the isHttps check):

HttpURLConnection urlConnection = (HttpURLConnection)new URL(url).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(60000);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

1

solved How to reduce redundant coding in java?