Web服务器是否可以向自身发出HTTPS请求? [英] Is it possible for a web server to make a HTTPS request to itself?
问题描述
假设您有两个应用程序,即A& B,在同一台Web服务器上运行. 您希望应用程序A通过SSL在应用程序B上调用网络服务.
Imagine you have two applications, A & B, running on the same web server. You want app A to call a webService on app B over SSL.
是否可以使用https://localhost/appsB/webService1
这样的地址来做到这一点?
Is it possible to do that with an address like https://localhost/appsB/webService1
?
如何在没有客户端(例如浏览器)的情况下完成SSL握手?
当使用此地址http://localhost/appsB/webService1
时,它实际上才起作用,仅在SSL模式下不起作用.
How can the SSL handshake be done without a client (like a browser?)
It actually works when using this address http://localhost/appsB/webService1
, only not in SSL mode.
但是,调用https://localhost/appsB/webService1
时,它在服务器和浏览器之间的HTTPS上也能很好地工作.
However it works as well with HTTPS between the server and a browser when calling https://localhost/appsB/webService1
.
现在,奇怪的是,它有时可以工作,但在使用HTTPS在应用B上调用webService时会随机失败.使用HTTP始终有效.
Now, the strange thing is that it works sometimes but randomly fails when calling the webService on app B using HTTPS. Using HTTP it always works.
我的测试是在IIS7.0上进行的,具有来自Thawte的有效ssl证书,并且应用B不需要SSL选项.
My tests are on IIS7.0 with a valid ssl certificate from Thawte with SSL option not required for app B.
以下是我的代码的一个示例:
Here is an exemple of my code :
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..
}
推荐答案
所有您需要做的就是在服务器A中创建一个https客户端以与自己对话.下面是我的代码.就我而言,是服务器A中的一个客户端与服务器A上的Web服务器接口进行通信.在这种情况下,我正在衡量服务器的延迟性能.
All you have to do is create a https client in server A to talk to talk to itself. Below is my code. In my case, it is a client in server A that talks to a webserver interface on Server A. In this case, I am measuring my servers latency performance.
// 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;
}
这篇关于Web服务器是否可以向自身发出HTTPS请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!