1. 程式人生 > >java呼叫webservice與介面方法

java呼叫webservice與介面方法

摘要: 本文講的是java呼叫webservice與介面方法, webservice的呼叫,常用的大約有3種方式:     1、使用axis呼叫     2、使用xfire呼叫     3、使用cxf呼叫     專案中,採用axis進行呼叫,記錄如下,備忘:   

 

webservice的呼叫,常用的大約有3種方式:

    1、使用axis呼叫

    2、使用xfire呼叫

    3、使用cxf呼叫

    專案中,採用axis進行呼叫,記錄如下,備忘:

    ps教程:想當年的時候是用的xfire方式呼叫的,結果沒做記錄,現在已經完全記不得怎麼玩了。所以說要多寫部落格啊 t_t

   版本說明:

        aixs版本:axis-bin-1_4.zip

        java環境略

    第一步:確保wsdl檔案可用,文中為獲取到sendsmsservice.wsdl,當然url的也行。

    第二步:執行生成客戶端程式碼的指令碼。指令碼內容為:

set axis_lib=d:axis-1_4lib

set java_cmd=java -djava.ext.dirs=%axis_lib%

set output_path=.

set package=info.jyzh.wap.liaoning.push

%java_cmd% org.apache.axis.wsdl.wsdl2java sendsmsservice.wsdl -o%output_path% -p%package% -t

 

#檢視wsdl2java的使用幫助#%java_cmd% org.apache.axis.wsdl.wsdl2java -help

 

ok,至此,客戶端程式碼就生成出來了。還帶了一個單元測試哦。

 

實際工作中,碰到以下情況,客戶端不能直接連上webservice伺服器,中間被強大的代理伺服器擋住了。如下圖:

 

java呼叫webservice與介面方法-java呼叫介面方法

    為此,修改生成的程式碼,本次是在sendmmsserviceimplservicesoapbindingstub中作修改,如下: 

   static {

            axisproperties.setproperty("http.proxyhost","88.88.88.88");

            axisproperties.setproperty("http.proxyport","8080");

            axisproperties.setproperty("http.proxyuser","asp教程yy");

            axisproperties.setproperty("http.proxypassword","123456");

        _operations = new org.apache.axis.description.operationdesc[1];

        _initoperationdesc1();

    }

直接axis呼叫遠端的web service

我覺得這種方法比較適合那些高手,他們能直接看懂xml格式的wsdl檔案,我自己是看不懂的,尤其我不是專門搞這行的,即使一段時間看懂,後來也就忘記了。直接呼叫模式如下:

import java.util.date;

import java.text.dateformat;

import org.apache.axis.client.call;

import org.apache.axis.client.service;

import javax.xml.namespace.qname;

import java.lang.integer;

import javax.xml.rpc.parametermode;



public class caclient {

            

       public static void main(string[] args) {



              try {

                     string endpoint = "http://localhost:8080/ca3/services/casynrochnized?wsdl";

                     //直接引用遠端的wsdl檔案

                    //以下都是套路 
                     service service = new service();

                     call call = (call) service.createcall();

                     call.settargetendpointaddress(endpoint);

                     call.setoperationname("adduser");//wsdl裡面描述的介面名稱

                     call.addparameter("username", org.apache.axis.encoding.xmltype.xsd_date,

                                   javax.xml.rpc.parametermode.in);//介面的引數

                     call.setreturntype(org.apache.axis.encoding.xmltype.xsd_string);//設定返回型別 


                     string temp = "測試人員";

                     string result = (string)call.invoke(new object[]{temp});

                     //給方法傳遞引數,並且呼叫方法

                     system.out.println("result is "+result);

              }

              catch (exception e) {

                     system.err.println(e.tostring());

              }

       }

}

2,直接soap呼叫遠端的webservice

這種模式我從來沒有見過,也沒有試過,但是網路上有人貼出來,我也轉過來

import org.apache.soap.util.xml.*;

import org.apache.soap.*;

import org.apache.soap.rpc.*;



import java.io.*;

import java.net.*;

import java.util.vector;



public class caservice{

       public static string getservice(string user) {

       url url = null;

       try {

           url=new url("http://192.168.0.100:8080/ca3/services/casynrochnized");

       } catch (malformedurlexception mue) {

          return mue.getmessage();

         }

             // this is the main soap object

       call soapcall = new call();

       // use soap encoding

       soapcall.setencodingstyleuri(constants.ns_uri_soap_enc);

       // this is the remote object we're asking for the price

       soapcall.settargetobjecturi("urn:xmethods-casynrochnized");

       // this is the name of the method on the above object

       soapcall.setmethodname("getuser");

       // we need to send the isbn number as an input parameter to the method

       vector soapparams = new vector();



       // name, type, value, encoding style

       parameter isbnparam = new parameter("username", string.class, user, null);

       soapparams.addelement(isbnparam);

       soapcall.setparams(soapparams);

       try {

          // invoke the remote method on the object

          response soapresponse = soapcall.invoke(url,"");

          // check to see if there is an error, return "n/a"

          if (soapresponse.generatedfault()) {

              fault fault = soapresponse.getfault();

             string f = fault.getfaultstring();

             return f;

          } else {

             // read result

             parameter soapresult = soapresponse.getreturnvalue ();

             // get a string from the result

             return soapresult.getvalue().tostring();

          }

       } catch (soapexception se) {

          return se.getmessage();

       }

    }

}

3,使用wsdl2java把wsdl檔案轉成本地類,然後像本地類一樣使用,即可。

以上是雲棲社群小編為您精心準備的的內容,在雲棲社群的部落格、問答、公眾號、人物、課程等欄目也有的相關內容,歡迎繼續使用右上角搜尋按鈕進行搜尋string , 檔案 , webservice , 介面 , new 程式碼 java呼叫介面方法、java反射呼叫介面方法、java呼叫介面的方法、java如何呼叫介面方法、java直接呼叫介面方法,以便於您獲取更多的相關知識。