Axis客户端调用Webservice_hanCSDN_20130225

来源:互联网 发布:双反相机知乎 编辑:程序博客网 时间:2024/05/29 16:47
import java.io.IOException;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.rpc.ParameterMode;import javax.xml.rpc.ServiceException;import javax.xml.soap.SOAPException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis.encoding.XMLType;public class NewAxisClient {private static final String STATUSCODE_SUCCESS = "0";private static final String STATUSCODE_ERROR1 =  "1";private static final String STATUSCODE_ERROR2 =  "2";private static final String STATUSCODE_ERROR3 =  "3";private static final String STATUSCODE_ERROR4 =  "4";private static final String STATUSCODE_ERROR5 =  "5";private static final String STATUSCODE_OTHTERS = "6";// urlprivate String endpoint = "http://localhost:9080/ws/services/Ws";// 命名空间private String namespace = "http://ws.test.com";// 调用的方法名private String method = "sendSms";public String getEndpoint() {return endpoint;}public void setEndpoint(String endpoint) {this.endpoint = endpoint;}public String getNamespace() {return namespace;}public void setNamespace(String namespace) {this.namespace = namespace;}public String getMethod() {return method;}public void setMethod(String method) {this.method = method;}public String sendSms(long id, String appKey, String phoneNums,String content, long time) throws SOAPException, IOException,InterruptedException, ServiceException {System.out.println("Initialize Service....");// 首先创建service对象Service service = new Service();System.out.println("Create Call object.");System.out.println("Create Call object."+"endpoint:"+endpoint);System.out.println("Create Call object.");System.out.println("Create Call object.");// 通过service对象来创建call对象Call call = (Call) service.createCall();// 设置web服务URLcall.setTargetEndpointAddress(new URL(endpoint));System.out.println("Set Operation.");// 设置操作方法名称,注意这里要明确命名空间,因为axis默认的命名空间与weblogic不同call.setOperationName(new javax.xml.namespace.QName(namespace, method));// 加入参数设置,同样要注意命名空间call.addParameter(new QName(namespace, "id"), XMLType.XSD_LONG,ParameterMode.IN);call.addParameter(new QName(namespace, "appKey"), XMLType.XSD_STRING,ParameterMode.IN);call.addParameter(new QName(namespace, "phoneNums"),XMLType.XSD_STRING, ParameterMode.IN);call.addParameter(new QName(namespace, "content"), XMLType.XSD_STRING,ParameterMode.IN);call.addParameter(new QName(namespace, "time"), XMLType.XSD_LONG,ParameterMode.IN);// 定义服务调用返回的数据类型call.setReturnType(XMLType.XSD_STRING);call.setSOAPActionURI(namespace + "/" + this.method);System.out.println("Invoke Service.");// 调用web服务,并返回结果String statusCode = (String) call.invoke(new Object[] {Long.valueOf(String.valueOf(id)), appKey, phoneNums, content,Long.valueOf(String.valueOf(time)) });return CodeToStr(statusCode);}public String CodeToStr(String code) {if ("0".equals(code)) {return STATUSCODE_SUCCESS;} else if ("1".equals(code)) {return STATUSCODE_ERROR1;} else if ("2".equals(code)) {return STATUSCODE_ERROR2;} else if ("3".equals(code)) {return STATUSCODE_ERROR3;} else if ("4".equals(code)) {return STATUSCODE_ERROR4;} else if ("5".equals(code)) {return STATUSCODE_ERROR5;} else {return STATUSCODE_OTHTERS;}}/** * 测试此类的主程序入口 *  * @param args *            命令行参数 *  */public static void main(String[] args) {NewAxisClient client = new NewAxisClient();client.setEndpoint("http://localhost:9080/ws/services/Ws");client.setNamespace("http://ws.test.com");client.setMethod("sendSms");try {String strXml = client.sendSms(1, "afdasfas", "11111111111","xxxxxxxx", System.currentTimeMillis());System.out.println(strXml);} catch (Exception se) {se.printStackTrace();}}}

原创粉丝点击