调用WebService的简单方法

时间:2023-03-09 04:58:24
调用WebService的简单方法
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");

如果对方还需要我们发送证书过去验证,发送前加载我们的密钥库。

            System.setProperty("javax.net.ssl.keyStore","D:/ssl/keystore/keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "123456");