Apache HTTP客户端 - 不想使用主机文件或DNS

时间:2017-11-02 16:13:48

标签: java apache ssl https hosts

我们正在编写一个通过Apache HTTP Client v4.5.2发送HTTPS POST消息的应用程序

我们已经让它在端点之间发送了一段时间的HTTP消息,但现在想要将其升级到HTTPS。

最终会有许多端点,每个端点都有一个唯一的主机名/ ID - 换句话说,它们的证书中有一个唯一的公用名。

在理想的世界中,我们可以确保主机文件或DNS与每个证书中的公用名匹配,以避免SSLPeerUnverifiedException

我们无法访问这些系统上的主机或DNS - 无需经过漫长的RFC流程(以及成本等等)。

我们的设计将所有主机和IP地址移动到应用程序数据库的表中。

我们遇到的问题是,与HTTP不同,我们无法选择相关的主机&来自数据库的IP并使用IP地址进行连接。

javax.net.ssl.SSLPeerUnverifiedException: Certificate for <127.0.0.1> doesn't match any of the subject alternative names: []

请有人告诉我是否有办法

  1. 从数据库中检索主机和IP
  2. 将它们传递给Apache HTTP Client
  3. 显示主机名而不是远程端的IP地址。
  4. 模仿使用主机/ DNS时会发生什么。

    在当前形式下,它可以正常使用HTTPS,但仅在进行主机文件更新时才能正常工作。

    代码:

    public static int post( String pid, String url, String paramKey, String paramVal ) throws ClientProtocolException, IOException
    {
            int responseCode = 0;
    
            CloseableHttpClient client = HttpClientBuilder.create( ).build( );
    
            HttpPost post = new HttpPost( url );
    
            post.setHeader( "User-Agent", USER_AGENT );
    
            List<NameValuePair> urlParameters = new ArrayList<NameValuePair>( );
    
            urlParameters.add( new BasicNameValuePair( paramKey, paramVal ) );
    
            post.setEntity( new UrlEncodedFormEntity( urlParameters, "UTF-8" ) );        
    
            HttpResponse response = client.execute( post );
    
            log.info( pid + " POST Response code : " + response );
    
            responseCode = response.getStatusLine( ).getStatusCode( );
    
            client.close( );
            client = null;
    
            return responseCode;
        }
    

0 个答案:

没有答案
相关问题