java 访问 https webservices

1、生成服务端密匙、证书
生成密匙 D:\apache-tomcat-6.0.16>keytool -genkey -alias tomcatserver -keyalg RSA -keypass changeit -storepass changeit -keystore tomcatserver.keystoreeystore tomcatserver.keystore
生成证书 D:\apache-tomcat-6.0.16>keytool -export -alias tomcatserver -storepass changeit -file tomcatserver.cer -keystore tomcatserver.keystore
导入证书文件到cacerts 文件中
D:\apache-tomcat-6.0.16>keytool -import -trustcacerts -alias tomcatserver -file
tomcatserver.cer -keystore tomcatserver

2、tomcat的server.xml配置
<Connector protocol="HTTP/1.1"
    port="8443" minSpareThreads="5" maxSpareThreads="75"
    enableLookups="true" disableUploadTimeout="true"
    acceptCount="100" maxThreads="200"
    scheme="https" secure="true" SSLEnabled="true"
    keystoreFile="D:\apache-tomcat-6.0.16\tomcatserver.keystore" keystorePass="changeit"
    truststoreFile="D:\apache-tomcat-6.0.16\tomcatserver"
    clientAuth="false" sslProtocol="TLS"/>

3、生成tomcatclient
keytool -import -trustcacerts -file tomcatserver.cer -keystore tomcatclient -storepass changeit

4、java代码 axis实现
System.setProperty("javax.net.ssl.trustStore","D:/apache-tomcat-6.0.16/tomcatclient");

Service service = new Service();
Call call = (Call) service.createCall();
String endpoint = "https://localhost:8443/services/AuthService";
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName("login");
call.addParameter("msg", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);

String msg = "79bb0325502b965a5c1e72d869d0e554d53089a25507480bc416147c411a86b3a09cc658de649dd96adb711264fe0247";
String isLogined = (String) call.invoke(new Object[] { msg });
System.out.println("Got result : " + isLogined);

你可能感兴趣的:(java,apache,tomcat,.net,xml)