專用輸出IP位址和VPN的HTTP/HTTPS連線

HTTP/HTTPS連線會自動從AEMas a Cloud Service使用專用輸出IP位址或VPN進行代理,不需要任何特殊處理 portForwards 規則。

進階網路支援

下列進階網路選項支援下列程式碼範例。

確保 專用輸出IP位址或VPN 在學習本教學課程之前,已設定進階網路設定。

CAUTION
此程式碼範例僅適用於 專用輸出IP位址VPN. 類似但不同的程式碼範例適用於 非標準連線埠上的HTTP/HTTPS連線,用於彈性連線埠輸出.

程式碼範例

此Java™程式碼範例屬於OSGi服務,可在AEMas a Cloud Service中執行,與8080上的外部網頁伺服器建立HTTP連線。 HTTPS (或HTTP)連線會自動以AEMas a Cloud Service代理,不需要特別開發。

NOTE
建議使用 Java™ 11 HTTP API 用於從AEM進行HTTP/HTTPS呼叫。
  • 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.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);

    // client with connection pool reused for all requests
    private HttpClient client = HttpClient.newBuilder().build();

    @Override
    public boolean isAccessible() {

        // 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;
        }
    }
}
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69