Java調用WebService(axis2)兩種方法

來源:互聯網
上載者:User

 

// 方式一:產生用戶端代碼調用方式。(使用此方法可以簡化很多代碼)
 // 
 // 通過外掛程式工具產生用戶端代碼進行調用。例如:存在一服務為
 // http://127.0.0.1:8000/axis2/services/HelloWorld?wsdl

//儲存為HelloWorld.wsdl.

自動產生了代碼,包括 HelloWorldProxy.java

public static void main(String[] args){

UploadProxy up = new UploadProxy();
  up.setEndpoint("http://127.0.0.1:70/uploadImg/services/UploadImg");

//圖片上傳fileinputstream省略
 up.updateImage("", uploadBuffer, 0, 0, "", "", "", "");}
 // 通過外掛程式可以產生SMSSendServiceStub.java。調用的用戶端代碼如:
 public static void main(String[] args) throws Exception {
  HelloWorldStub stub;
  stub = new HelloWorldStub();
  HelloWorldStub.GetName helloWorld = new HelloWorldStub.GetName();//
  helloWorld.setName("peipan");
  System.out.println(stub.getName(helloWorld).get_return());

 }

//  方式二:使用axis2.rpc.client.RPCServiceClient方式調用。
//  public static void main(String[] args)  throws Exception  {
//  GetWSByAxis2 ws = new GetWSByAxis2();
//  ws.WSUrl = "http://127.0.0.1:8000/axis2/services/HelloWorld";
//  String result;
//  try {
//  result = ws.getStr("getName", "peipan");
//  System.out.println(result);
//  } catch (AxisFault e) {
//  e.printStackTrace();
//  }
//  }

-------------------------------------------------

package test;
import javax.xml.namespace.QName;   

import org.apache.axis2.AxisFault;   
import org.apache.axis2.addressing.EndpointReference;   
import org.apache.axis2.client.Options;   
import org.apache.axis2.rpc.client.RPCServiceClient;   

public class GetWSByAxis2 {
       private static String EndPointUrl;   
       private static String QUrl="http://ws.apache.org/axis2"; 
       private QName opAddEntry;    
       public String WSUrl;   
       public RPCServiceClient setOption() throws AxisFault   
       {   
           RPCServiceClient serviceClient = new RPCServiceClient();   
           Options options = serviceClient.getOptions();   
           EndpointReference targetEPR = new EndpointReference(WSUrl);   
           options.setTo(targetEPR);   
           return serviceClient;   
       }   
          
       public QName getQname(String Option){   
              
           return new QName (QUrl,Option);   
       }   
  //返回String   
       public String getStr(String Option,String why) throws AxisFault   
       {   
           RPCServiceClient serviceClient =this.setOption();    
         
           opAddEntry =this.getQname(Option);   
          
          String str = (String) serviceClient.invokeBlocking(opAddEntry,    
                           new Object[]{why}, new Class[]{String.class })[0];   
          return str;   
      }   
 // 返回一維String數組   
       public String[] getArray(String Option) throws AxisFault   
       {   
           RPCServiceClient serviceClient =this.setOption();    
         
           opAddEntry =this.getQname(Option);   
          
          String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,    
                           new Object[]{}, new Class[]{String[].class })[0];   
          return strArray;   
      }   
       //從WebService中返回一個對象的執行個體   
      public Object getObject(String Option,Object o) throws AxisFault   
      {    
         RPCServiceClient serviceClient =this.setOption();    
          QName qname=this.getQname(Option);   
          Object object = serviceClient.invokeBlocking(qname, new Object[]{},new Class[]{o.getClass()})[0];   
          return object;   
      }   
         
 /////////////////////////////////////////       讀者可以自己封裝資料類型,如int,byte,float等資料類型   
 }  

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.