Several examples of Java invoke WebService (ASMX) __java

Source: Internet
Author: User

Wrote a few invocation examples:

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 = (call) Service.createcall ();
Call.settargetendpointaddress (New Java.net.URL (EndPoint));
Call.setoperation ("Getversiontime");
Call.setusesoapaction (TRUE);
Call.setsoapactionuri ("Http://WebXml.com.cn/getVersionTime");
Call.setoperationname (New QName (""));
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= "http://WebXml.com.cn/";

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[]{});//calling method and passing 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= "provides an address for an interface
Private String soapaction= "http://WebXml.com.cn/"; 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 call
Call.addparameter (New QName (SOAPAction, "Thecityname"),//Set parameters to pass
Org.apache.axis.encoding.XMLType.XSD_STRING,
Javax.xml.rpc.ParameterMode.IN);
Call.setreturntype (New QName (SOAPAction, "Getweatherbycityname"), Vector.class); The data type 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});//calling method and passing 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.