Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy response from Jsoup.connect.execute is null? when throwing IOException?
    primarykey
    data
    text
    <p>My code look like this:</p> <p>When I try to invoke this method with incorrect url e.g. <a href="http://en.dddddddddssss.org/" rel="nofollow">http://en.dddddddddssss.org/</a> execute throw exception and response is null. Why? How can I got http code in that situation?</p> <pre><code>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; } } </code></pre> <p>EDIT</p> <pre><code>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() : "&lt;null&gt;", response != null ? String.valueOf(response.statusCode()) : "&lt;null&gt;" }); throw new SiteBusinessException(response != null ? response.statusMessage() : "&lt;null&gt;", String.valueOf(response != null ? response.statusCode() : "&lt;null&gt;")); } catch (IOException ioe) { LOGGER.warn("IOException. Cannot fetch site [url={}, errorMessage={}]", url, ioe.getMessage()); throw new SiteBusinessException("Not found"); } } </code></pre> <p>And then I'm trying to call <code>http://localhost:8090/wrongaddress/</code>. Jboss return HTTP 404. </p> <p>But my code return </p> <pre><code>Cannot fetch site [url=http://localhost:8090/wrongaddress/, statusMessage=&lt;null&gt;, statusCode=&lt;null&gt;] </code></pre> <p>EDIT</p> <p>WORKING SOLUTION </p> <pre><code>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"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload