Are you sure you're not getting the FileNotFound when trying to do getInputStream on the connection later (or in your real code in case this is a simplified example)? Since HttpUrlConnection implements http protocol and 4xx codes are error codes trying to call getInputStream generates FNF. Instead you should call getErrorStream. I ran your code (without the auth part) and I don't get any FilenotFoundException testing on url's that return 404. So in this case HttpUrlConnection correctly implements the http protocol and appdynamics correctly catches the error. I'm facing the same issue with jersey wrapping the FnF in a UniformResourceException but after some analyzing it's actually either jersey that should provide ways of checking the status code before returning output or correctly use httpurlconnection, and in our case - the webservice should not return 404 for requests that yields no found results but rather an empty collection. import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Connection {
public boolean connect(URL url, String requestType) {
HttpURLConnection connection = null;
try {
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
connection.setRequestMethod(requestType);
}
if (connection == null) {
return false;
}
connection.connect();
System.out.println(connection.getResponseCode());
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
} catch (IOException e) {
System.out.println("E" + e.getMessage());
return false;
}
}
public static void main(String args[])
{
Connection con = new Connection();
try{
con.connect(new URL("http://www.google.com/asdfasdfsd"), "GET");
} catch(MalformedURLException mfe)
{
}
}
}
... View more