package com.xxx.webservice.internal.test; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class T { public static String callWebService(String serviceUrl,String methodName,String postMsg) { postMsg = (postMsg==null?"":postMsg); URL url = null; String rs = ""; try { url = new URL(serviceUrl); Service service = new Service(); // 通過service創建call對象 Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url); call.setOperationName(methodName); Object rsObject = call.invoke(new Object[]{postMsg}); if(rsObject!=null){ rs = (String)rsObject; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } return rs; } public static void main(String[] args) { String rs = callWebService("http://localhost:8080/TestService/services/TestWebService?wsdl","doPay", null); System.out.println(rs); } }
如果調用地址是https的,可以再發送前加載包含訪問地址證書的信任證書庫。
System.setProperty("javax.net.ssl.trustStore","D:/ssl/truststore/keystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
或者把要信任的證書導入到jdk的證書庫
keytool -import -alias xxxxxx -file "D:/xxxxxx.cer" -keystore "D:/Program Files (x86)/jdk1.8.0_77/jre/lib/security/cacerts" -storepass changeit -trustcacerts
如果對方還需要我們發送證書過去驗證,發送前加載我們的密鑰庫。
System.setProperty("javax.net.ssl.keyStore","D:/ssl/keystore/keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "123456");
附上證書庫查看命令和刪除命令
keytool -list -v -keystore "D:/Program Files (x86)/jdk1.8.0_77/jre/lib/security/cacerts" -storepass changeit
keytool -delete -alias xxxxxx -keystore "D:/Program Files (x86)/jdk1.8.0_77/jre/lib/security/cacerts" -storepass changeit
