如何在我的 Java Webservice Client Request 中使用 SSL/TLS 握手



Im 使用 Java HTTP Web 服务客户端作为 POST 请求服务器(使用 text/xml 请求格式)。实际上它在我的系统中工作正常。但是,在服务器端,他们无法从我这边收到任何请求。服务器不在我们的控制之下(其政府相关服务器,他们使用SSL/TLS握手以确保安全)。服务器团队提供了握手的密钥。

实际上我不知道如何使用SSL/TLS以及如何在我的代码中添加它。服务器团队告诉,他们将在握手成功后收到请求。

我现在被困住了。是否需要在我的 Tomcat 服务器上添加证书?如何在我的请求中添加SSL握手?如果有人知道SSL/TLS握手,请帮助我提示。

提前谢谢..

我的代码如下:

  public File sendRequest(File req_xml, String responseFileName) {
    File xmlFile = new File(responseFileName);
    try {
        OutputStream outputStream = new FileOutputStream(xmlFile);
        HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
        httpClient.getParams().setParameter("http.useragent",
                "Web Service Test Client");
        BufferedReader br = null;
        PostMethod methodPost = new PostMethod("http://192.168.1.53:8080/MyApplication/service");
        try {
            methodPost.setRequestBody(new FileInputStream(req_xml));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        methodPost.setRequestHeader("Content-Type", "text/xml");
        try {
            int returnCode = httpClient.executeMethod(methodPost);
            if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                System.out
                        .println("The Post method is not implemented by this URI");
                methodPost.getResponseBodyAsString();
            } else {
                br = new BufferedReader(new InputStreamReader(
                        methodPost.getResponseBodyAsStream()));
                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = methodPost.getResponseBodyAsStream().read(
                        bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            methodPost.releaseConnection();
            if (br != null)
                try {
                    br.close();
                } catch (Exception fe) {
                    fe.printStackTrace();
                }
        }
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    return xmlFile;
}
PostMethod methodPost = new PostMethod("http://192.168.1.53:8080/MyApplication/service");

只需将其更改为

PostMethod methodPost = new PostMethod("https://192.168.1.53:8080/MyApplication/service");

我得到了SSL问题的解决方案。Tomcat 将为 SSL 握手做一切事情。我们需要在我们的系统中安装我们的证书。并在我们的应用程序中添加以下属性:

System.setProperty("javax.net.ssl.keyStore", "Our Installed Key Path");
System.setProperty("javax.net.ssl.keyStorePassword", "keystore password");
System.setProperty("javax.net.ssl.trustStore", "our trust store");
System.setProperty("javax.net.ssl.trustStorePassword", "trust store pwd");
System.setProperty("javax.net.ssl.trustStoreType", "trust store type");

最新更新