Returns an HTTP response. @return response @throws IOException I/O exception
()
| 132 | * @throws IOException I/O exception |
| 133 | */ |
| 134 | public HttpResponse<InputStream> response() throws IOException { |
| 135 | final HttpClient client = client(true); |
| 136 | |
| 137 | final HttpResponse<InputStream> response; |
| 138 | try { |
| 139 | final URI uri = new URI(pth); |
| 140 | final HttpRequest.Builder rb = HttpRequest.newBuilder(uri).timeout(Duration.ofMinutes(1)); |
| 141 | rb.header(HTTPText.ACCEPT, MediaType.ALL_ALL.toString()); |
| 142 | rb.header(HTTPText.USER_AGENT, Prop.NAME + '/' + Prop.VERSION.replace(' ', '-') + |
| 143 | " (Java " + Prop.JAVA_VERSION + "; " + Prop.OS + " " + Prop.OS_ARCH + ')'); |
| 144 | new UserInfo(uri).basic(rb); |
| 145 | response = client.send(rb.build(), HttpResponse.BodyHandlers.ofInputStream()); |
| 146 | } catch(final IOException ex) { |
| 147 | throw ex; |
| 148 | } catch(final Exception ex) { |
| 149 | /* possible exceptions, among others: |
| 150 | * - construct URI: invalid argument |
| 151 | * - build request: invalid URI scheme |
| 152 | * - send request: interrupted requests */ |
| 153 | Util.debug(ex); |
| 154 | throw new IOException(ex.getMessage()); |
| 155 | } |
| 156 | |
| 157 | // create exception if response was not successful |
| 158 | final int status = response.statusCode(); |
| 159 | if(status >= 400) { |
| 160 | final StringBuilder sb = new StringBuilder().append(status); |
| 161 | final String reason = reason(status); |
| 162 | if(!reason.isEmpty()) sb.append(": ").append(reason); |
| 163 | throw new IOException(sb.toString()); |
| 164 | } |
| 165 | return response; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns a singleton HTTP client instance. |