【问题标题】:Is it possible for a web server to make a HTTPS request to itself?Web 服务器是否可以向自身发出 HTTPS 请求?
【发布时间】:2015-05-27 23:26:41
【问题描述】:

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

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

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

但是,当调用https://localhost/appsB/webService1 时,它也适用于服务器和浏览器之间的 HTTPS。

现在,奇怪的是它有时可以工作,但在使用 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..
}

【问题讨论】:

  • 如果您的意思是将客户端重定向到另一个链接,您可以使用 iis 重定向非常简单,只需添加功能即可
  • 不,我的意思是从服务器向同一服务器但不同的应用程序发出 https 请求;)
  • 您为 *.somesite.com 生成证书,该证书适用于与您的站点或应用程序相关的所有文件
  • 证书适用于安装了这两个应用程序的一个站点。但我认为我的解释很糟糕。我只是想知道 appA 是否可以在同一个站点和同一个 webServer 上请求 appB。这是服务器以 HTTPS 向自身发出的请求,但向不同的应用程序发出请求。它在 http 中有效,但在 https 中无效,我想知道为什么.. 向 localhost 请求请求 https 协议可能完全没用,但我在文档中没有找到任何证据..
  • 可以编写一个应用程序来访问 localhost 上的 https。您可以发布不起作用的代码吗?您可能需要在 appA 中提供自定义主机名验证逻辑...类似于:stackoverflow.com/questions/2675133/…

标签: iis https request localhost server


【解决方案1】:

您所要做的就是在服务器 A 中创建一个 https 客户端来与自己交谈。下面是我的代码。在我的例子中,它是服务器 A 中的客户端与服务器 A 上的网络服务器接口进行通信。在这种情况下,我正在测量我的服务器延迟性能。

// 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;
    }

【讨论】:

    猜你喜欢
    • 2018-01-14
    • 2012-08-26
    • 2020-03-18
    • 1970-01-01
    • 2018-10-04
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多