非標準埠(不是80/443)上的HTTP/HTTPS連接必須從as a Cloud Service中,但它們不需要任何特殊 portForwards
規則,並可AEM以使用 AEM_HTTP_PROXY_HOST
。 AEM_HTTP_PROXY_PORT
。 AEM_HTTPS_PROXY_HOST
, AEM_HTTPS_PROXY_PORT
。
以下高級網路選項支援以下代碼示例。
確保 適當 本教程之後,高級網路配置已設定完畢。
無高級網路 | 靈活的埠出口 | 專用出口IP地址 | 虛擬專用網路 |
---|---|---|---|
✘ | ✘ | ✔ | ✔ |
此代碼示例僅用於 專用出口IP地址 和 虛擬專用網。 類似但不同的代碼示例可用於 非標準埠上的HTTP/HTTPS連接,用於靈活埠輸出。
此Java™代碼示例是OSGi服務的示例,該服務可以在AEMas a Cloud Service中運行,該在8080上與外部Web伺服器建立HTTP連接。 與HTTPS Web伺服器的連接使用 AEM_HTTPS_PROXY_HOST
和 AEM_HTTPS_PROXY_PORT
而不是 AEM_HTTP_PROXY_HOST
和 AEM_HTTP_PROXY_PORT
。
建議 Java™ 11 HTTP API 用於從中進行HTTP/HTTPS調AEM用。
core/src/com/adobe/aem/wknd/examples/connections/impl/HttpExternalServiceImpl.java
package com.adobe.aem.wknd.examples.core.connections.impl;
import com.adobe.aem.wknd.examples.core.connections.ExternalService;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
@Component
public class HttpExternalServiceImpl implements ExternalService {
private static final Logger log = LoggerFactory.getLogger(HttpExternalServiceImpl.class);
@Override
public boolean isAccessible() {
HttpClient client;
// If the URL is http, use System.getenv("AEM_HTTP_PROXY_HOST") and System.getenv("AEM_HTTP_PROXY_PORT")
// Else if the URL is https, us System.getenv("AEM_HTTPS_PROXY_HOST") and System.getenv("AEM_HTTPS_PROXY_PORT")
if (System.getenv("AEM_HTTP_PROXY_HOST") != null) {
// Create a ProxySelector that maps to AEM's provided AEM_HTTP_PROXY_HOST and AEM_HTTP_PROXY_PORT
ProxySelector proxySelector = ProxySelector.of(
new InetSocketAddress(System.getenv("AEM_HTTP_PROXY_HOST"),
Integer.parseInt(System.getenv("AEM_HTTP_PROXY_PORT"))));
// Create an HttpClient and provide the proxy selector that will use AEM's native HTTP proxy configuration
client = HttpClient.newBuilder().proxy(proxySelector).build();
log.debug("Using HTTPClient with AEM_HTTP_PROXY");
} else {
client = HttpClient.newBuilder().build();
// If no proxy is set up (such as local dev)
log.debug("Using HTTPClient without AEM_HTTP_PROXY");
}
// Prepare the full URI to request, note this will have the
// - Scheme (http/https)
// - External host name
// - External port
// The external service URI, including the scheme/host/port, is defined in code, rather than in Cloud Manager portForwards rules.
URI uri = URI.create("http://api.example.com:8080/test.json");
// Prepare the HttpRequest
HttpRequest request = HttpRequest.newBuilder().uri(uri).timeout(Duration.ofSeconds(2)).build();
// Send the HttpRequest using the configured HttpClient
HttpResponse<String> response = null;
try {
// Request the URL
response = client.send(request, HttpResponse.BodyHandlers.ofString());
log.debug("HTTP response body: {} ", response.body());
// Our simple example returns true is response is successful! (200 status code)
return response.statusCode() == 200;
} catch (IOException e) {
return false;
} catch (InterruptedException e) {
return false;
}
}
}