如何在我的Java Webservice客户端请求中使用SSL / TLS握手

时间:2014-12-10 06:32:36

标签: java web-services ssl

我使用Java HTTP webservice客户端请求服务器作为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;
}

2 个答案:

答案 0 :(得分:0)

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

只需将其更改为

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

答案 1 :(得分:0)

我找到了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");