当前位置: 代码迷 >> Web前端 >> java调用webservice范例 Axis
  详细解决方案

java调用webservice范例 Axis

热度:502   发布时间:2012-12-28 10:29:05.0
java调用webservice实例 Axis
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 
import java.util.Vector; 
import javax.xml.namespace.QName; 
/**<dl>
 *<dt>类名:MyWebServices</dt>
 *<dd>描述:Axis调用Webservice</dd>  
 *<dd>创建时间:May 5, 2012 11:46:20 PM </dd>
 *<dd>创建人:zj </dd>
 *</dl>
 */
public class MyWebServices {
	private String url = "http://www.mxtong.net.cn/GateWay/Services.asmx";//提供接口的地址 

	private String soapaction = "http://tempuri.org/"; //域名,这是在server定义的 

	public MyWebServices()

	{

		String UserID = "876890";

		String Account = "admin";

		String Password = "******";

		String Phones = "134********;1234";

		String Content = "hello,this is a test!这是个测试!";

		String SendTime = "2011-9-5 16:07:00";

		String SendType = "1";

		String PostFixNumber = "1";

		Service service = new Service();

		try {

			Call call = (Call) service.createCall();

			call.setTargetEndpointAddress(url);

			call.setOperationName(new QName(soapaction, "DirectSend")); //设置要调用哪个方法 

			call.addParameter(new QName(soapaction, "UserID"), //设置要传递的参数 

					org.apache.axis.encoding.XMLType.XSD_STRING,

					javax.xml.rpc.ParameterMode.IN);

			call.addParameter(new QName(soapaction, "Account"), //设置要传递的参数 

					org.apache.axis.encoding.XMLType.XSD_STRING,

					javax.xml.rpc.ParameterMode.IN);

			call.addParameter(new QName(soapaction, "Password"), //设置要传递的参数 

					org.apache.axis.encoding.XMLType.XSD_STRING,

					javax.xml.rpc.ParameterMode.IN);

			call.addParameter(new QName(soapaction, "Phones"), //设置要传递的参数 

					org.apache.axis.encoding.XMLType.XSD_STRING,

					javax.xml.rpc.ParameterMode.IN);

			call.addParameter(new QName(soapaction, "Content"), //设置要传递的参数 

					org.apache.axis.encoding.XMLType.XSD_STRING,

					javax.xml.rpc.ParameterMode.IN);

			call.addParameter(new QName(soapaction, "SendTime"), //设置要传递的参数 

					org.apache.axis.encoding.XMLType.XSD_STRING,

					javax.xml.rpc.ParameterMode.IN);

			call.addParameter(new QName(soapaction, "SendType"), //设置要传递的参数 

					org.apache.axis.encoding.XMLType.XSD_STRING,

					javax.xml.rpc.ParameterMode.IN);

			call.addParameter(new QName(soapaction, "PostFixNumber"), //设置要传递的参数 

					org.apache.axis.encoding.XMLType.XSD_STRING,

					javax.xml.rpc.ParameterMode.IN);

			call.setReturnType(new QName(soapaction, "DirectSend"),
					Vector.class); //要返回的数据类型(自定义类型) 

			//            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//(标准的类型) 

			call.setUseSOAPAction(true);

			call.setSOAPActionURI(soapaction + "DirectSend");

			Vector v = (Vector) call.invoke(new Object[] { UserID, Account,
					Password, Phones, Content, SendTime, SendType,
					PostFixNumber });//调用方法并传递参数         

			for (int i = 0; i < v.size(); i++)

			{

				System.out.println(v.get(i));

			}

		} catch (Exception ex)

		{

			ex.printStackTrace();

		}

	}

	public static void main(String args[])

	{

		MyWebServices pw = new MyWebServices();

	}

}

  相关解决方案