【问题标题】:Why response from Jsoup.connect.execute is null? when throwing IOException?为什么 Jsoup.connect.execute 的响应为空?抛出 IOException 时?
【发布时间】:2013-11-09 08:27:11
【问题描述】:

我的代码如下所示:

当我尝试使用不正确的 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");
        }

【问题讨论】:

    标签: java parsing jsoup


    【解决方案1】:

    不,它没有。您正在捕获异常并自己返回 null。你永远不能在抛出异常的同时返回一些东西。

    不会有 HTTP 代码,因为主机不存在。 HTTP 代码由服务器返回。例如,最著名的代码是 404(未找到)。当您的浏览器显示 404 时,它只是服务器发送给客户端的 HTTP/TCP 数据包,其中包含此代码。

    【讨论】:

    • 这个空值是不合适的。我正在查看调试模式。
    猜你喜欢
    • 2021-03-05
    • 2012-01-13
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    相关资源
    最近更新 更多