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