用于专用出口IP地址和VPN的HTTP/HTTPS连接

使用专用出口IP地址或VPN,HTTP/HTTPS连接会自动从AEMas a Cloud Service中代理,它们不需要任何特殊 portForwards 规则。

高级网络支持

以下高级网络选项支持以下代码示例。

确保 专用出口IP地址或VPN 在完成本教程之前,已设置高级网络配置。

无高级网络 灵活的端口出口 专用出口IP地址 虚拟专用网
注意

此代码示例仅适用于 专用出口IP地址VPN. 有一个相似但不同的代码示例可供使用 非标准端口上的HTTP/HTTPS连接用于灵活端口出口.

代码示例

此Java™代码示例是OSGi服务的一个示例,该服务可以在AEMas a Cloud Service中运行,该服务在8080上与外部Web服务器进行HTTP连接。 HTTPS(或HTTP)连接会自动从AEMas a Cloud Service中代理,无需进行特殊开发。

注意

建议使用 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;
        }
    }
}

在此页面上