无法通过 host = null 上的 java 异常
Unable to get past a java exception on host = null
我正在尝试确定 url 是好是坏。我无法通过主机为空的 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);
}
}
也许有人可以在他们的 eclipse 上测试它,看看他们是否可以复制错误或者只有我。
更新
这是我尝试在上述异常中 return 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;
}
}
方法继续抛出异常。我被困在这里,无法通过这个异常。
您的 URL 出了点问题。如果我 运行 你的代码使用 http://www.example.com/
而不是 hulkshare url,它就会越过你卡住的那条线。 (由于不同的原因,它最终失败了,在你已经收到 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
和 return 错误。我怀疑您根本没有捕捉到抛出的 RuntimeException
。只需在 catch
子句中撒下更大的网:
} catch (RuntimeException | IOException e) {
return false;
}
我正在尝试确定 url 是好是坏。我无法通过主机为空的 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);
}
}
也许有人可以在他们的 eclipse 上测试它,看看他们是否可以复制错误或者只有我。
更新
这是我尝试在上述异常中 return 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;
}
}
方法继续抛出异常。我被困在这里,无法通过这个异常。
您的 URL 出了点问题。如果我 运行 你的代码使用 http://www.example.com/
而不是 hulkshare url,它就会越过你卡住的那条线。 (由于不同的原因,它最终失败了,在你已经收到 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
和 return 错误。我怀疑您根本没有捕捉到抛出的 RuntimeException
。只需在 catch
子句中撒下更大的网:
} catch (RuntimeException | IOException e) {
return false;
}