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 找個美女做老婆 閱讀(15635) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

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

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 浦城县| 涿州市| 乌拉特中旗| 五大连池市| 旬邑县| 龙陵县| 平和县| 沁源县| 易门县| 张掖市| 西丰县| 高唐县| 万州区| 钦州市| 宜宾县| 鹤峰县| 吉木乃县| 宜都市| 岳西县| 平泉县| 永年县| 红原县| 枣庄市| 元江| 内乡县| 定襄县| 清远市| 稷山县| 南丹县| 泰来县| 昂仁县| 厦门市| 子洲县| 上蔡县| 定西市| 额尔古纳市| 淮滨县| 东安县| 阿克苏市| 弥渡县| 和平区|