我正在尝试确定网址是好还是坏。我无法通过HttpURLConnection超出主机为空的异常。
我的代码如下:
URL url;
UrlValidator validate = new UrlValidator();
if(validate.isValid(rs.getString("url"))){
url = new URL(rs.getString("url"));
System.out.println(url.getHost().length());
if(url.getHost().length() == 0){//testing purposes
System.out.println("BAD"); //testing purposes
System.exit(0);//testing purposes
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(connection != null){
int rc = connection.getResponseCode();
System.out.println(rc);
/*
connection.setRequestMethod("HEAD");
connection.connect();
String contentType = connection.getContentType();
//if(contentType.contains("audio") || contentType.contains("mp3") || contentType.contains("mpeg")){
System.out.println(contentType+" == "+rs.getString("url"));
//}
}
我继续得到这个例外
Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalArgumentException: protocol = http host = null
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1241)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:2696)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:477)
我不介意例外,但我希望能够忽略它而且我不能。
这是我正在使用的导致错误的确切代码。
url = new URL("http://www.hulkshare.com/dl/epsbxff31peb/waka_flocka_flame_ft_drake-round_of_applause_%28remix%29.mp3");
if(url.getHost() != null){
System.out.println("Good Url");
//System.exit(0);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(connection != null){
int rc = connection.getResponseCode();
System.out.println(rc);
connection.setRequestMethod("HEAD");
connection.connect();
String contentType = connection.getContentType();
System.out.println(contentType);
}
}
也许有人可以在他们的日食上测试它,看看他们是否可以复制错误,或者它只是我。
更新
这是我尝试在上述异常中返回false的方法。
public static boolean isValidURL(){
URL url;
try {
url = new URL("http://www.hulkshare.com/dl/epsbxff31peb/waka_flocka_flame_ft_drake-round_of_applause_%28remix%29.mp3");
//System.out.println(url.getHost());
if(url.getHost().length() == 0 || url.getHost() == null){
System.out.println("BAD");
//System.exit(0);
return false;
}else{
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(connection != null){
int rc = connection.getResponseCode();
System.out.println(rc);
connection.setRequestMethod("HEAD");
connection.connect();
String contentType = connection.getContentType();
//if(contentType.contains("audio") || contentType.contains("mp3") || contentType.contains("mpeg")){
System.out.println(contentType);
//}
}
return true;
}
} catch (IllegalArgumentException | IOException e) {
//e.printStackTrace();
return false;
}
}
该方法继续抛出异常。我被困在这里,无法超越这个例外。
答案 0 :(得分:1)
您的网址有问题。如果我使用http://www.example.com/
而不是hulkshare网址运行您的代码,则会超过您坚持使用的那一行。 (在您已经收到HTTP响应代码后,尝试将方法设置为HEAD
时,最终会因为不同的原因而失败几行,而您无法做到这一点。)
如果我尝试在命令行上获取该文件,它也会失败:
$ wget "http://www.hulkshare.com/dl/epsbxff31peb/waka_flocka_flame_ft_drake-round_of_applause_%28remix%29.mp3"
--2015-01-21 11:05:11-- http://www.hulkshare.com/dl/epsbxff31peb/waka_flocka_flame_ft_drake-round_of_applause_%28remix%29.mp3
Resolving www.hulkshare.com... 109.201.151.6, 109.201.151.5, 109.201.151.3, ...
Connecting to www.hulkshare.com|109.201.151.6|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: //?force=1 [following]
http://?force=1: Invalid host name.
我现在通过您的评论和问题编辑了解到,您真正想要做的就是捕获Exception
并返回false。我怀疑你根本没有抓住RuntimeException
被抛出的东西。只需在catch
子句中添加更广泛的网络:
} catch (RuntimeException | IOException e) {
return false;
}