2011年9月9日 星期五

jsp 呼叫 .Net 所寫的 WebService

這裡需要先當露 jsp 所需要的元件 Axis ,目前本人的環境為 Axis1.4版+Apache Tomcat 6.0+Java 7(?)

下載完後,解壓縮資料夾,把 webapps\axis\WEB-INF\lib 目錄下的所有檔案,複製到 TomCat的 webapps\ROOT\WEB-INF\lib 裡面,同樣的,把 webapps\axis\WEB-INF\classes 複製到TomCat的webapps\ROOT\WEB-INF\classes裡面

編輯環境變數


新增
名稱:AXIS_HOME            值:%TOMCAT_HOME%\webapps\axis
名稱:AXIS_LIB                 值:%AXIS_HOME%\lib
名稱:AXISCLASSPATH  值:%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery.jar; %AXIS_LIB%\commons-logging.jar;%AXIS_LIB%\jaxrpc.jar; %AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar; %AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar

嗯。。。。。有點麻煩吧。。。

接下來,把剛剛解壓縮的 Axis目錄打開,把webapps目錄下的axis目錄,整個複製到
TomCat的 webapps 下,在瀏覽器上打

http://自己湯姆貓的網址/axis ,會出現測試網頁


接下來,都沒問題的話,在瀏覽器上打
http://自己湯姆貓的網址/axis/happyaxis.jsp ,會出現測試的結果


沒什麼大問題的話,就可以開始享受寫Code的樂趣了

以下是.net的WebService

VB.net:

    _
    Public Function TestHello(ByVal strName As String) As String
        Try
            My.Computer.FileSystem.WriteAllText("D:\tmp\test.txt", strName & vbCrLf, True)
        Catch ex As Exception

        End Try
        Return "Hello" & strName
    End Function
End Class


我們這次的測試目標擺在最後一個 Function (TestHello),測試方式很簡單,我傳進一個名稱,回傳 『Hello』+ 『傳入名稱』,例如傳入『 Randy笨蛋』,會回傳 『Hello Randy笨蛋』

接下來是JSP的 code

JSP:

需要傳入的套件
org.apache.axis.client.Call
org.apache.axis.client.Service
javax.xml.namespace.QName
java.util.Date
java.text.DateFormat
java.lang.Integer
javax.xml.rpc.ParameterMode

主要的code:

String strEndpoint = "http://localhost/TestWS/service1.asmx";
//注意,這個NameSpace一定要設定,不然傳進的參數永遠都NULL
String strNameSpace="http://localhost/TestWS/";

Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(strEndpoint));
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://localhost/TestWS/TestHello");
call.setOperationName(new QName(strNameSpace,"TestHello"));
call.addParameter(new QName(strNameSpace,"strName"), org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
String result;
call.setTargetEndpointAddress(new java.net.URL(strEndpoint));
result=(String) call.invoke(new Object[] {" Randy笨蛋"});


=result


請注意

String strNameSpace="http://localhost/TestWS/";
call.setOperationName(new QName(strNameSpace,"TestHello"));
call.addParameter(new QName(strNameSpace,"strName"), org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN); 

傳錯了,所有String 的參數都是NULL,所有Integer的參數都是0,意思是萬劫不復,永不超生的地獄就此展開,永遠的一路靠北--邊走。。。Debug到天荒地老也是沒結果,就跟把正妹一樣,而且這裡的錯誤是沒錯誤訊息....就如同被女生拒絕也不知道,被發好人卡卻又可以約出去玩,超級大大大好人,別再犯這個錯,親愛的網友們,注意這個微不足道的小重點吧。。


所以我們期望的結果是『Hello Randy笨蛋』

以下就是執行結果



出現的是『Hello Randy�³J 』,沒錯,編碼問題,編碼問題本人有時間在試試 @@...

3 則留言:

  1. Effectively, this publish is in fact a freshest subject
    matter within this personal computer registry linked matter.
    I fit in with your current results and can thirstily count on
    your current forthcoming changes. Simply indicating thanks a
    lot won't just be all you need, for any incredible lucidity in your producing. I am going to without delay get hold of your current feed to keep abreast of any changes.
    Feel free to surf my web blog ... Background Investigation

    回覆刪除
  2. Guess what happens, you generally are a great read through nonetheless these
    times I feel want it is put jointly a little quick.

    I'd personally always be improper that but it really just simply doesn’t pass such as other discussions. Sorry this.
    Feel free to surf my site ; Remove Genital Warts

    回覆刪除
  3. Thanks for finally talking about > "jsp 呼叫 .Net 所寫的 WebService" < Loved it!

    http://amateur-swingers-posts.com/forums/profile.php?mode=viewprofile&u=1634293
    http://api.mybuzzboard.com/forums/topic/buy-twitter-followers-at-affordable-price-any-rewards/
    http://amanjohn.com/forums/topic/how-can-i-get-more-twitter-followers/
    http://anotherserver.co.uk/laureatedvd/profile.php?mode=viewprofile&u=151284
    http://api.mybuzzboard.com/forums/topic/quick-technologies-with-benefits-of-social-media-marketing-to-a-good-modify/

    my web-site jordan basketball shoes

    回覆刪除