3
\$\begingroup\$

I have code that take in a URL (via URL or String) and returns the status code. I am new to web development, so it is very possible that I may be taking a poor approach at this. One thing I don't like about my code is the need for the InetSocketAddress to be hardcoded, but I have not been able to find a way around this short of passing it in as parameters. As an inexperienced programmer and complete newbie to web development, what improvements to the following code would you suggest?

import java.io.IOException;
import java.net.*;

public class HttpStatusCodeRequester {
    public static int getHttpStatusCode(URL url) {
        try {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.mycompany.com", 8099));
            HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
            connection.setRequestMethod("GET");
            connection.connect();
            int responseCode = connection.getResponseCode();
            connection.disconnect();
            return responseCode;
        } catch (IOException e) {
            System.err.println("HTTP Status Code Request failed for " + url + " in HttpStatusCodeRequester.getHttpStatusCode(URL)!");
            e.printStackTrace();
            return -1;
        }
    }

    public static int getHttpStatusCode(String url) {
        try {
            return getHttpStatusCode(new URL(url));
        } catch (MalformedURLException e) {
            System.err.println("Malformed URL passed into HttpStatusCodeRequester.getHttpStatusCode(String)!");
            e.printStackTrace();
            return -1;
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Have you looked at servlets? I think that's the way to go for you. \$\endgroup\$ – Thufir Feb 12 '15 at 19:50
1
\$\begingroup\$

You could use Properties and read from them... IMHO, I think almost everything that goes quoted ("like this") can be either a constant or sent to a Properties file, you could have one for messages and one for configuration for example.

Also, I've noticed that you have two return statements per method, I would recommend to have only one, you don't really need one inside the catch block since the exception is suppossed to be handled then returned. It is also good for readability.

Bear in mind that exceptions could happen before you reach the connection.disconnect(); statement so I would recommend a try, catch, finally and perform all your clean up un the finally block.

Another thing... I think it is better to handle the exception differently depending on the scenario or error that caused it, for example, a connection couldn't been stablished, wrong URL, timeout (I don't know), etc.

\$\endgroup\$
  • 3
    \$\begingroup\$ Welcome to Code Review! Could you clarify what you mean by "since the exception is suppossed to be handled then returned"? \$\endgroup\$ – 200_success Feb 12 '15 at 21:53
  • \$\begingroup\$ Properties are a good idea. In fact, there are standard properties for configuring the HTTP proxy. \$\endgroup\$ – 200_success Feb 12 '15 at 22:08
  • \$\begingroup\$ @200_success Thanks! Handled like in: [code]System.err.println("Malformed URL passed into HttpStatusCodeRequester.getHttpStatusCode(String)!"); e.printStackTrace();[code] I would first do: int returnCode = -1 // responseCode or whatever is descriptive enough. try { . . returnCode = connection.getResponseCode(); catch(Exception ex) { . . } return returnCode; IMHO it's easier to read if there is only one return per method. Thanks! Regards! \$\endgroup\$ – Ban Finch Apr 15 '15 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.