webservice客户端访问服务的几种方式
1.最简的一种
根据对方提供的wsdl生成本地java类,访问webservice就跟调用本地类没有什么区别了.
2.使用xfire的其中的一种调用方式,代码如下:
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
public class Test
{
public void test() throws MalformedURLException, Exception
{
Client client = new Client(new URL("http://58.58.33.33/sqtwebservice/UserManage.asmx?wsdl"));
Object[] resultXMLObjs = client.invoke("GetCurfee", new Object[] {"huy88888", "123456" });
System.out.println(resultXMLObjs[0]);
}
public static void main(String[] args) throws MalformedURLException, Exception
{
Test test = new Test();
test.test();
}
}
3.使用axis的其中一种调用方式:
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
/** *//**
* <ul>
* <li>Title:[ProviderJiangXiTelecom]</li>
* <li>Description: [江西电信-webservice]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-1-7]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class ProviderJiangXiTelecom
{
public void Webservice1()
{
try
{
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL("http://58.58.33.33/sqtwebservice/UserManage.asmx"));
call.setOperationName(new QName("http://tempuri.org/", "GetCurfee"));
call.addParameter("uname", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("pass", XMLType.XSD_STRING, ParameterMode.IN);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/GetCurfee");
call.setReturnType(XMLType.XSD_DECIMAL);
BigDecimal result = (BigDecimal) call.invoke(new Object[] { "huy88888","123456" });
System.out.println(result.doubleValue());
}
catch (ServiceException e)
{
e.printStackTrace();
}
catch (RemoteException e)
{
e.printStackTrace();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ProviderJiangXiTelecom providerJiangXiTelecom = new ProviderJiangXiTelecom();
providerJiangXiTelecom.Webservice1();
}
}