2008-11-28 67 views
11

是否有人知道,如果有的话,如果服务器启用了SSL,我该如何检查应用代码?如何检查服务器是否启用了SSL?

+0

你能提供一些信息。关于你写这个的环境? – 2008-11-28 12:42:54

+0

你不能。服务器没有ssl,网站做。 – Kuzgun 2013-12-03 15:27:18

回答

8

"It's easier to ask forgiveness than permission"

例如,读通过SSL stackoverflow.com,不问stackoverflow.com是否支持它,只是做到这一点。在Python中:

>>> import urllib2 
>>> urllib2.urlopen('https://stackoverflow.com') 
Traceback (most recent call last): 
... 
urllib2.URLError: <urlopen error (10060, 'Operation timed out')> 
>>> html = urllib2.urlopen('http://stackoverflow.com').read() 
>>> len(html) 
146271 
>>> 

这表明stackoverflow.com不支持SSL(2008)。


更新:stackoverflow.com现在支持HTTPS。

2

不知道自己的偏好的语言,但在这里它是在C#

public bool IsSecureConnection() 
{ 
    return HttpContext.Current.Request.IsSecureConnection || 
      HttpContext.Current.Request.Headers["HTTP_X_SSL_REQUEST"].Equals("1"); 
} 

请注意,这头是定制的,但我觉得你的想法。我见过人们只是简单地查询“https”的请求,除了看起来很脏,它可能是合理可接受的,取决于你的安全模型。

或者你问它是否简单可用?

+0

Thx鸽子,我认为是在服务器端,我想知道在客户端如何知道服务器是否接受SSL连接。 (如果我能以某种方式)。我想我可以做一些异常处理,如果我尝试使用ssl enable连接到服务器并且出现异常,那么服务器(可能)没有启用ssl。但我能以其他方式做到吗? – 2008-11-28 12:41:12

0

你需要指定你正在使用何种协议 - 有HTTP,IMAP,POP的SSL版本等

假设它是HTTPS您有兴趣,你可以检查看看是否有在服务器侦听端口443,并从那里......

5

您不指定编程语言,但可以从命令行执行此操作。

bash-3.2$ echo ^D | telnet www.google.com https 
Trying 66.102.11.104... 
Connected to www.l.google.com. 
Escape character is '^]'. 
Connection closed by foreign host. 
bash-3.2$ echo ^D | telnet www.stackoverflow.com https 
Trying 69.59.196.211... 
telnet: connect to address 69.59.196.211: Connection refused 
telnet: Unable to connect to remote host 

你去......谷歌呢,StackOverflow没有。

0

如果您在服务器上运行PHP或ASP代码,那么最简单的答案就是您不需要。您可以尝试建立与非ssl IP地址的套接字连接,并查看是否获得ssl证书,并枚举其Common Name和SubjectAlternativeNames,但通常情况下,简单答案是不。 apache频繁(错误)配置是在没有SSL证书的情况下在端口443上侦听,因此能够进行连接并不能保证那里存在SSL。无法建立连接可能意味着您的应用程序没有网络权限。因为设置SSL是一种痛苦,所以您知道您是否拥有SSL,这是一个配置决定。这就像想知道你有多少孩子 - 你应该知道的。

0

这是一个C#单元测试,而无需在右边的HttpContext进行检测:

[TestMethod] 
    public void DetectSslSupport() 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.someinsecuresite.com"); 
     try 
     { 
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       //some sites like stackoverflow will perform a service side redirect to the http site before the browser/request can throw an errror. 
       Assert.IsTrue(response.ResponseUri.Scheme == "https"); 
      } 
     } 
     catch (WebException)//"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."} 
     { 
      Assert.IsTrue(false); 
     } 
    } 
相关问题