| 139 | } |
| 140 | |
| 141 | HttpURLConnection convertAndSend(Request request, Options options) throws IOException { |
| 142 | final URL url = new URL(request.url()); |
| 143 | final HttpURLConnection connection = this.getConnection(url); |
| 144 | if (connection instanceof HttpsURLConnection) { |
| 145 | HttpsURLConnection sslCon = (HttpsURLConnection) connection; |
| 146 | if (sslContextFactory != null) { |
| 147 | sslCon.setSSLSocketFactory(sslContextFactory); |
| 148 | } |
| 149 | if (hostnameVerifier != null) { |
| 150 | sslCon.setHostnameVerifier(hostnameVerifier); |
| 151 | } |
| 152 | } |
| 153 | connection.setConnectTimeout(options.connectTimeoutMillis()); |
| 154 | connection.setReadTimeout(options.readTimeoutMillis()); |
| 155 | connection.setAllowUserInteraction(false); |
| 156 | connection.setInstanceFollowRedirects(options.isFollowRedirects()); |
| 157 | connection.setRequestMethod(request.httpMethod().name()); |
| 158 | |
| 159 | Collection<String> contentEncodingValues = request.headers().get(CONTENT_ENCODING); |
| 160 | boolean gzipEncodedRequest = this.isGzip(contentEncodingValues); |
| 161 | boolean deflateEncodedRequest = this.isDeflate(contentEncodingValues); |
| 162 | |
| 163 | boolean hasAcceptHeader = false; |
| 164 | Integer contentLength = null; |
| 165 | for (String field : request.headers().keySet()) { |
| 166 | if (field.equalsIgnoreCase("Accept")) { |
| 167 | hasAcceptHeader = true; |
| 168 | } |
| 169 | for (String value : request.headers().get(field)) { |
| 170 | if (field.equals(CONTENT_LENGTH)) { |
| 171 | if (!gzipEncodedRequest && !deflateEncodedRequest) { |
| 172 | contentLength = Integer.valueOf(value); |
| 173 | connection.addRequestProperty(field, value); |
| 174 | } |
| 175 | } |
| 176 | // Avoid add "Accept-encoding" twice or more when "compression" option is enabled |
| 177 | else if (field.equals(ACCEPT_ENCODING)) { |
| 178 | connection.addRequestProperty(field, String.join(", ", request.headers().get(field))); |
| 179 | break; |
| 180 | } else { |
| 181 | connection.addRequestProperty(field, value); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | // Some servers choke on the default accept string. |
| 186 | if (!hasAcceptHeader) { |
| 187 | connection.addRequestProperty("Accept", "*/*"); |
| 188 | } |
| 189 | |
| 190 | byte[] body = request.body(); |
| 191 | |
| 192 | if (body != null) { |
| 193 | /* |
| 194 | * Ignore disableRequestBuffering flag if the empty body was set, to ensure that internal |
| 195 | * retry logic applies to such requests. |
| 196 | */ |
| 197 | if (disableRequestBuffering) { |
| 198 | if (contentLength != null) { |