Web 服务器是否可以向自身发出 HTTPS 请求?

Gui*_*éan 5 iis https localhost request server

假设您有两个应用程序 A 和 B,在同一 Web 服务器上运行。您希望应用程序 A 通过 SSL 调用应用程序 B 上的 webService。

是否可以使用类似的地址来做到这一点https://localhost/appsB/webService1

如何在没有客户端(如浏览器)的情况下完成 SSL 握手?它实际上在使用此地址时有效http://localhost/appsB/webService1,只是在 SSL 模式下不起作用。

然而,在调用时,它可以与服务器和浏览器之间的 HTTPS 配合使用https://localhost/appsB/webService1

现在,奇怪的是,它有时可以工作,但在使用 HTTPS 调用应用程序 B 上的 webService 时会随机失败。使用HTTP它总是有效的。

我的测试是在 IIS7.0 上进行的,具有来自 Thawte 的有效 ssl 证书,并且应用程序 B 不需要 SSL 选项。

这是我的代码的示例:

string baseAddress = "http://localhost";
//or string baseAddress = "https://localhost";
var baseUri = new Uri(baseAddress);
//final url for appB
finalUrl = baseAddress + httpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/" + url;
//Add a cookie to retrieve good contexte on appB
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseUri })
{
  cookieContainer.Add(baseUri, new Cookie("ASP.NET_SessionId", HttpContext.Current.Session.SessionID));
  HttpResponseMessage response = client.GetAsync(finalUrl).Result;
  Dictionary<string, dynamic> sData;
if (response.IsSuccessStatusCode)
{
    etc, etc..
}
Run Code Online (Sandbox Code Playgroud)

skm*_*eld 2

您所要做的就是在服务器 A 中创建一个 https 客户端来与自身对话。下面是我的代码。就我而言,服务器 A 中的客户端与服务器 A 上的 Web 服务器接口进行通信。在本例中,我正在测量服务器延迟性能。

// Get Administration Server Status
    public String adminServerStatus() {

        uri = "https://" + "localhost" + ":" + adminserverport + "/home";
        result = "grn";
        adminlatency = 0;

        // Build httpHeader entity
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> httpentity = new HttpEntity<String>(headers);

        try {

            // Build the httpClient
            TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
            SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                .loadTrustMaterial(null, acceptingTrustStrategy)
                .build();

            SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
            CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();

            // Build httpClient template
            HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory();
            requestFactory.setHttpClient(httpClient);               
            RestTemplate template = new RestTemplate(requestFactory);

            // Now go fire client status request & get latency duration
            timenow = System.currentTimeMillis();
            ResponseEntity<String> httpresult = template.exchange(uri, HttpMethod.GET, httpentity, String.class);
            adminlatency = System.currentTimeMillis() - timenow;

            HttpStatus statuscode = httpresult.getStatusCode();
            if (statuscode != HttpStatus.OK) {
                result = "yel";
                adminlatency = 0;
            }

            httpClient.close();

        // Error Caught
        } catch (Exception e) {
            result = "red";
            adminlatency = 0;
        }

        return result;
    }
Run Code Online (Sandbox Code Playgroud)