Java学习

java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已经搬家了,新的地址是 http://www.javaly.cn 如果有对文章有任何疑问或者有任何不懂的地方,欢迎到www.javaly.cn (Java乐园)指出,我会尽力帮助解决。一起进步

 

Java调用Webservice(asmx)的几个例子


zhuan(http://www.yexu8.com/article.asp?id=1038)
写了几个调用例子:

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("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="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[]{});//调用方法并传递参数        
            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";//提供接口的地址
    private String soapaction="http://WebXml.com.cn/";   //域名,这是在server定义的
    
    public PutWeather()
    {
        String City="北京";        
        Service service=new Service();
        try{
            Call call=(Call)service.createCall();            
            call.setTargetEndpointAddress(url);            
            call.setOperationName(new QName(soapaction,"getWeatherbyCityName")); //设置要调用哪个方法
            call.addParameter(new QName(soapaction,"theCityName"), //设置要传递的参数
                    org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.setReturnType(new QName(soapaction,"getWeatherbyCityName"),Vector.class); //要返回的数据类型(自定义类型)
            
//            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//(标准的类型)
            
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapaction + "getWeatherbyCityName");    
                        
            Vector v=(Vector)call.invoke(new Object[]{City});//调用方法并传递参数        
            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();
    }
    
    
}

其中第三个是转载的,可惜,地址不知道了……

--!Sorry

写这个以防以后想用的时候找不到例子~~http://WebXml.com.cn/这个是个不错的webservice地址 当然是免费的了。

呵呵~~~突然发现这个东西不错,比写b/s程序好多了,给用户提供的接口多,丰富,安全,可扩展性好……现在的web技术真的是越来越好了!

黑客发明网络的第一天,就注定了网络主宰地球的一天。

posted on 2009-04-08 17:47 找个美女做老婆 阅读(15067) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

本blog已经搬到新家了, 新家:www.javaly.cn
 http://www.javaly.cn

常用链接

留言簿(6)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜