in sdk/core/azure-core-http-httpurlconnection/src/main/java/com/azure/android/core/http/httpurlconnection/HttpUrlConnectionAsyncHttpClient.java [64:174]
private void sendIntern(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
if (cancellationToken.isCancellationRequested()) {
httpCallback.onError(new IOException("Canceled."));
return;
}
final HttpURLConnection connection;
try {
connection = (HttpURLConnection) httpRequest.getUrl().openConnection();
} catch (IOException ioe) {
httpCallback.onError(ioe);
return;
}
connection.setDoInput(true);
Throwable error = null;
HttpResponse httpResponse = null;
boolean hasResponseContent = false;
try {
// Request: headers
for (HttpHeader header : httpRequest.getHeaders()) {
connection.addRequestProperty(header.getName(), header.getValue());
}
// Request: method and content.
switch (httpRequest.getHttpMethod()) {
case GET:
case HEAD:
case OPTIONS:
case TRACE:
connection.setRequestMethod(httpRequest.getHttpMethod().toString());
break;
case PUT:
case POST:
case PATCH:
case DELETE:
connection.setRequestMethod(httpRequest.getHttpMethod().toString());
final byte[] requestContent = httpRequest.getBody();
if (requestContent != null) {
connection.setDoOutput(true);
final OutputStream requestContentStream = connection.getOutputStream();
try {
requestContentStream.write(requestContent);
} finally {
requestContentStream.close();
}
}
break;
default:
throw logger.logExceptionAsError(new IllegalStateException("Unknown HTTP Method:"
+ httpRequest.getHttpMethod()));
}
// Response: StatusCode
final int statusCode = connection.getResponseCode();
if (statusCode == -1) {
final IOException ioException = new IOException("Retrieval of HTTP response code failed. "
+ "HttpUrlConnection::getResponseCode() returned -1");
throw logger.logExceptionAsError(new RuntimeException(ioException));
}
// Response: headers
final Map<String, List<String>> connHeaderMap = connection.getHeaderFields();
final HttpHeaders headers = new HttpHeaders();
for (Map.Entry<String, List<String>> entry : connHeaderMap.entrySet()) {
if (entry.getKey() == null) {
continue;
}
final String headerName = entry.getKey();
final String headerValue;
Iterator<String> hdrValueItr = entry.getValue().iterator();
if (hdrValueItr.hasNext()) {
headerValue = hdrValueItr.next();
} else {
headerValue = null;
}
headers.put(headerName, headerValue);
}
// Response: Content
hasResponseContent = statusCode != HttpURLConnection.HTTP_NO_CONTENT
&& statusCode != HttpURLConnection.HTTP_NOT_MODIFIED
&& statusCode >= HttpURLConnection.HTTP_OK
&& httpRequest.getHttpMethod() != HttpMethod.HEAD;
final InputStream responseContentStream = hasResponseContent
? new ResponseContentStream(connection)
: new ByteArrayInputStream(new byte[0]);
httpResponse = new UrlConnectionResponse(logger,
httpRequest,
statusCode,
headers,
responseContentStream);
} catch (Throwable e) {
error = e;
} finally {
if (error != null || !hasResponseContent) {
connection.disconnect();
}
}
if (error != null) {
httpCallback.onError(error);
return;
} else {
httpCallback.onSuccess(httpResponse);
}
}