天天看點

Java調用Webservice(asmx)的例子(實際應用)

package com.segsec.gisap.webservice;

import java.util.Vector;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

public class LbsRescue {

	private String url = "http://IP位址/LbsRescue.asmx";// 提供接口的位址
	private String soapaction = "http://www.***.net:7005/"; // 域名,這是在server定義的

	public LbsRescue() {
		String City = "北京";
		Service service = new Service();
		try {
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new java.net.URL(url));
			call.getMessageContext().setUsername("ygbx");// axis中的使用者名。
			call.getMessageContext().setPassword("5kY3XhfxyNnHPtwrhvDycQ==");// 密碼
			call.setUseSOAPAction(true);
			call.setSOAPActionURI(soapaction + "sendList");
			call.setOperationName(new QName(soapaction, "sendList")); // 設定要調用哪個方法
			call.addParameter(
					new QName(soapaction, "companyid"), // 設定要傳遞的參數
					org.apache.axis.encoding.XMLType.XSD_STRING,
					javax.xml.rpc.ParameterMode.IN);
			call.addParameter(
					new QName(soapaction, "companypwd"), // 設定要傳遞的參數
					org.apache.axis.encoding.XMLType.XSD_STRING,
					javax.xml.rpc.ParameterMode.IN);
			call.addParameter(
					new QName(soapaction, "mobile"), // 設定要傳遞的參數
					org.apache.axis.encoding.XMLType.XSD_STRING,
					javax.xml.rpc.ParameterMode.IN);
			call.addParameter(
					new QName(soapaction, "flag"), // 設定要傳遞的參數
					org.apache.axis.encoding.XMLType.XSD_STRING,
					javax.xml.rpc.ParameterMode.IN);
			
			call.setReturnType(XMLType.XSD_STRING); // 要傳回的資料類型(自定義類型)

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

			call.setUseSOAPAction(true);
			call.setSOAPActionURI(soapaction + "sendList");

			String s = (String) call.invoke(new Object[] {"ygbx","pfAo/msenzv8lChXTPa9yw==","15910510352",1});// 調用方法并傳遞參數
			System.out.println("注冊手機:"+s);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String args[]) {
		LbsRescue LR = new LbsRescue();
	}

}