Axis客户端调用Webservice_hanCSDN_20130225


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";

	// url
	private 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服务URL
		call.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();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值