我的代码如下所示:
当我尝试使用不正确的 URL 调用此方法时,例如http://en.dddddddddssss.org/执行抛出异常并且响应为空。为什么?在那种情况下我怎样才能得到http代码?
public Document getDocumentFromUrl(String url) throws SiteBusinessException {
Response response = null;
try {
response = Jsoup.connect(url).timeout(Constans.TIMEOUT).ignoreHttpErrors(false).userAgent(Constans.USER_AGENT)
.ignoreContentType(Constans.IGNORE_CONTENT_TYPE).execute();
return response.parse();
} catch (IOException ioe) {
LOGGER.warn("Cannot fetch site ]");
return null;
}
}
编辑
public Document getDocumentFromUrl(String url) throws SiteBusinessException {
Response response = null;
try {
response = Jsoup.connect(url).timeout(Constans.TIMEOUT).ignoreHttpErrors(false)
.userAgent(Constans.USER_AGENT).ignoreContentType(Constans.IGNORE_CONTENT_TYPE).execute();
return response.parse();
} catch (HttpStatusException hse) {
LOGGER.warn("Cannot fetch site [url={}, statusMessage={}, statusCode={}]",
new Object[] { url, response != null ? response.statusMessage() : "<null>",
response != null ? String.valueOf(response.statusCode()) : "<null>" });
throw new SiteBusinessException(response != null ? response.statusMessage() : "<null>",
String.valueOf(response != null ? response.statusCode() : "<null>"));
} catch (IOException ioe) {
LOGGER.warn("IOException. Cannot fetch site [url={}, errorMessage={}]", url, ioe.getMessage());
throw new SiteBusinessException("Not found");
}
}
然后我试着打电话http://localhost:8090/wrongaddress/
。Jboss 返回 HTTP 404。
但我的代码返回
Cannot fetch site [url=http://localhost:8090/wrongaddress/, statusMessage=<null>, statusCode=<null>]
编辑
工作解决方案
try {
response = Jsoup.connect(url).execute();
return processDocument(response.parse(), url);
} catch (IllegalArgumentException iae) {
LOGGER.warn("Malformed URL [url={}, message={}]", new Object[] { url, iae.getMessage() });
throw new SiteBusinessException(iae.getMessage());
} catch (MalformedURLException mue) {
LOGGER.warn("Malformed URL [url={}, message={}]", new Object[] { url, mue.getMessage() });
throw new SiteBusinessException(mue.getMessage());
} catch (HttpStatusException hse) {
LOGGER.warn("Cannot fetch site [url={}, statusMessage={}, statusCode={}]",
new Object[] { url, hse.getMessage(), hse.getStatusCode() });
throw new SiteBusinessException(hse.getMessage(), hse.getStatusCode());
} catch (IOException ioe) {
LOGGER.warn("IOException. Cannot fetch site [url={}, errorMessage={}]", url, ioe.getMessage());
throw new SiteBusinessException("Cannot fetch site");
}