java遠端呼叫WebService方法
阿新 • • 發佈:2019-01-08
通過AXIS呼叫遠端的web service。已測可用。
import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; /** * WebService呼叫 通過AXIS呼叫遠端的web service * * 需要用到的架包有axis.jar和jaxrpc.jar * * @author Thinkpad * */ public class WebServiceInvoke { public static void main(String[] args) { String endpoint = "http://localhost/test/webservice/dataExchangeService?wsdl";// 遠端請求訪問wsdl檔案 String xmlBody = "<?xml version=\"1.0\" encoding=\"GBK\"?><request>" + "<id> 201807060001</id>" + "<sfz>110101198001010010</sfz>" + "</request>";// 請求引數 String returnData; try { Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint);// 遠端呼叫路徑 call.setOperationName("doService");// 呼叫的方法名 // 設定引數名: 引數名,引數型別,引數模式 call.addParameter("service", XMLType.XSD_STRING, ParameterMode.IN); call.addParameter("xmlBody", XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(XMLType.XSD_STRING);// 設定被呼叫方法的返回值型別 returnData = (String) call.invoke(new Object[] { xmlBody });// 遠端呼叫 System.out.println("result is " + returnData); } catch (Exception e) { System.err.println(e.toString()); } } }