I am creating a utility class to do an HttpURLConnection. I want to make this class generic as in I pass the URL and the POST params. It works with any range of these two values.public class HttpUrlConnection {    String url, params;    public HttpUrlConnection(String url, String params) {        this.url = url;        this.params = params;        try {            sendPost();        }catch(Exception e){            System.out.println(Exception+e);        }    }    private String sendPost() throws Exception {        String USER_AGENT = Mozilla/5.0;        URL obj = new URL(url);        HttpURLConnection con = (HttpURLConnection) obj.openConnection();        //add reuqest header        con.setRequestMethod(POST);        con.setRequestProperty(User-Agent, USER_AGENT);        con.setRequestProperty(Content-Type, application/x-www-form-urlencoded);        String urlParameters = params;        // Send post request        con.setDoOutput(true);        DataOutputStream wr = new DataOutputStream(con.getOutputStream());        wr.writeBytes(urlParameters);        wr.flush();        wr.close();        int responseCode = con.getResponseCode();        System.out.println(\nSending 'POST' request to URL :  + url);        System.out.println(Post parameters :  + urlParameters);        System.out.println(Response Code :  + responseCode);        BufferedReader in = new BufferedReader(                new InputStreamReader(con.getInputStream()));        String inputLine;        StringBuffer response = new StringBuffer();        while ((inputLine = in.readLine()) != null) {            response.append(inputLine);        }        in.close();        //print result        System.out.println(rvsp + response.toString());        return response.toString();    }}I am unsure if I am following the best practice above.Should I keep the cosntructor short as: public HttpUrlConnection(String url, String params) {        this.url = url;        this.params = params;    }and start the POST using connection.sendPost where in some other class I do: HttpUrlConnection connection;connection = new HttpUrlConnection(url,params);connection.sendPost();
Utility class for an HttpURLConnection
java;http;constructor
String url, params;They should be private and final and each declared on its own line. However... they just get passed from the constructor to the method. There's a single use. So you can drop them and pass them directly.The next step is to drop the object and use a static method.}catch(Exception e){        System.out.println(Exception+e);    }If eating exceptions was such a good idea as many think, no JDK method would throw anything. Imagine openConnection printing something like HeghlumeH QaQ jajvam or Connection reset by peer to standard output and everyone happily looking at the console to tell their program what to do.At the very very least, use e.printStackTrace. But the simplest solution is nearly always the best: Declare you method to throw and let it be!sendPost();You're throwing away the returned value.In case of an exception you're leaking resources, use try-with-resources or alike to ensure everything gets closed.Utility classA typical utility class is java.lang.Math:private constructorno instancesno fields but static final constants