0

我是一个完整的初学者,我已经尝试与服务器建立连接已有一段时间了

public class Test {

    public static void main(String[] args) throws ClientProtocolException, IOException {

    DefaultHttpClient httpClient = new DefaultHttpClient();

        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope("9.5.127.34", 80),
                new UsernamePasswordCredentials("root", "passw0rd"));

        String url_copied_from_firebug = "https://9.5.127.34/powervc/openstack/volume/v1/115e4ad38aef463e8f99991baad1f809//volumes/3627400b-cd98-46c7-a7e2-ebce587a0b05/restricted_metadata"
        HttpGet httpget = new HttpGet(url_copied_from_firebug);
        HttpResponse response = httpClient.execute(httpget);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {

            System.out.println(line);

            }   
        }
}

我尝试运行代码时遇到的错误是

线程“主”javax.net.ssl.SSLPeerUnverifiedException 中的异常:对等体未通过身份验证

我尝试将端口号从 80 更改为 443,但它不起作用。我想我是从它开始的,可能会遗漏很多东西。请指出我正确的方向。

提前谢谢。

4

2 回答 2

1

您的问题不是 HTTP 身份验证。您的 http 客户端无法使用证书颁发机构验证服务器的 ssl 证书 - 这可能是因为您使用的是自签名证书。

查看 HttpClient文档以获取有关如何自定义客户端以允许自签名证书的说明。

下面是一个创建接受所有证书和主机名的 HttpClient 的示例 - 请记住将它与您信任的服务器一起使用:

    private DefaultHttpClient getSSLHttpClient(final URL url) throws RestClientException {
    try {
        final X509TrustManager trustManager = createTrustManager();
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[]{trustManager}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, createHostnameVerifier());
        AbstractHttpClient base = new DefaultHttpClient();
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme(HTTPS, url.getPort(), ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (final Exception e) {
        throw new RestClientException(FAILED_CREATING_CLIENT, "Failed creating http client",
                ExceptionUtils.getFullStackTrace(e));
    }
}


private X509TrustManager createTrustManager() {
    X509TrustManager tm = new X509TrustManager() {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(final X509Certificate[] chain, final String authType) 
                throws CertificateException {
        }

        @Override
        public void checkClientTrusted(final X509Certificate[] chain, final String authType) 
                throws CertificateException {
        }
    };
    return tm;
}


private X509HostnameVerifier createHostnameVerifier() {
    X509HostnameVerifier verifier = new X509HostnameVerifier() {

        @Override
        public boolean verify(final String arg0, final SSLSession arg1) {
            return true;
        }

        @Override
        public void verify(final String host, final String[] cns, final String[] subjectAlts) 
                throws SSLException {
        }

        @Override
        public void verify(final String host, final X509Certificate cert) 
                throws SSLException {
        }

        @Override
        public void verify(final String host, final SSLSocket ssl) 
                throws IOException {
        }
    };
    return verifier;
}
于 2013-10-29T12:13:25.777 回答
0

首先你必须配置 server.xml 文件。你必须取消注释哪一行 start Connector port="8443"

于 2013-10-29T12:19:43.823 回答