A few examples of Java calling WebService (asmx) 2009-06-28 17:07
Several invocation examples were written:
1.
Import org.apache.axis.client.*;
Import org.apache.axis.client.Call;
Import org.apache.axis.client.Service;
Import javax.xml.namespace.QName;
Import javax.xml.rpc.ParameterMode;
Import javax.xml.rpc.JAXRPCException;
Import javax.xml.rpc.ServiceFactory;
public class TestWebService {
/**
* @param args
*/
public static void main (string[] Args) {
TODO auto-generated Method Stub
System.out.println ("Start Invoking ....");
Try
{
String endpoint= "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx";
Service service = new service ();
Call Call Service.createcall ();
Call.settargetendpointaddress (new Java.net.URL (endPoint));
Call.setoperation ("getversiontime");
Call.setusesoapaction (true);
Call.setsoapactionuri ("");
Call.setoperationname (new QName ("www.webxml.com.cn", "getversiontime"));
Call.setreturntype (org.apache.axis.encoding.XMLType.XSD_STRING);
String str= (string) call.invoke (new object[]{});
System.out.println (str);
}catch (Exception E)
{
E.printstacktrace ();
}
}
}
2.
Import java.util.LinkedList;
Import java.util.List;
Import java.util.Map;
Import java.util.Vector;
Import javax.xml.namespace.QName;
Import org.apache.axis.client.Call;
Import org.apache.axis.client.Service;
public class Getipcontent
{
Private String url= "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx";
Private String soapaction= "";
Public Getipcontent ()
{
Service Service=new Service ();
try{
Call call= (call) Service.createcall ();
Call.settargetendpointaddress (url);
Call.setoperationname (new QName (soapaction, "getgeoipcontext"));
Call.setreturntype (new QName (soapaction, "getgeoipcontext"), vector.class);
Call.setusesoapaction (true);
Call.setsoapactionuri (soapaction + "getgeoipcontext");
Vector v= (vector) call.invoke (new Object[]{});//call method and pass parameters
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[])
{
Getipcontent gip=new getipcontent ();
}
}
3.
Import java.util.LinkedList;
Import java.util.List;
Import java.util.Map;
Import java.util.Vector;
Import javax.xml.namespace.QName;
Import org.apache.axis.client.Call;
Import org.apache.axis.client.Service;
public class Putweather
{
Private String url= "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";//address that provides the interface
Private String soapaction= ""; Domain name, which is defined in the server
Public Putweather ()
{
String city= "beijing";
Service Service=new Service ();
try{
Call call= (call) Service.createcall ();
Call.settargetendpointaddress (url);
Call.setoperationname (new QName (soapaction, "getweatherbycityname")); Set which method to invoke
Call.addparameter (new QName (soapaction, "thecityname"),//set parameters to be passed
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
Call.setreturntype (new QName (soapaction, "getweatherbycityname"), vector.class); The type of data to return (custom Type)
Call.setreturntype (org.apache.axis.encoding.XMLType.XSD_STRING);//(standard Type)
Call.setusesoapaction (true);
Call.setsoapactionuri (soapaction + "getweatherbycityname");
Vector v= (vector) call.invoke (new Object[]{city});//call method and pass parameters
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[])
{
Putweather pw=new Putweather ();
}
}
The third one is reproduced, unfortunately, the address does not know ...
--! Sorry
Write this in case you want to use later when you can not find an example ~ ~ This is a good WebService address of course is free of charge.
hehe ~ ~ ~ Suddenly found this thing is good, than write b/s program much better, to provide users with many interfaces, rich, safe, scalability is good ... Today's Web technology is really getting better!
Hackers invented the first day of the network, it is doomed to network dominate the Earth day.
A few examples of Java calling WebService (asmx)