当前位置:编程学习 > JSP >>

教您在J2ME中访问dotnet Web Services

答案:经过大量的试验证明,目前最方便、快速的方式就是通过JSR172规范来实现对Web Services的访问,可以访问其它任何工具创建的Web Services。

目前,有两种方式访问Web服务:

1、通过JSR172 API。

2、通过KSOAP API。

由于使用KSOAP方式访问Web服务的例子很多,尤其是访问使用Java开发的Web服务,但是使用KSOAP方式访问用.NET 开发的Web服务,目前的例子很少,而且我花了几天时间没有调通一个,原因未明,故只讨论使用JSR172的方式。KSOAP的方式这里暂不讨论。

注:要获知KSOAP的详细资料请上:http://ksoap.objectweb.org/

要获知JSR172的资料请上:http://java.sun.com/products/wsa/

本示例开发环境:J2ME Wireless Toolkit 2.2,JB9,dotnet2003

需要的jar包:kxml-min.zip ,ksoap-midp.zip

步骤1:使用.NET 开发的Web 服务为:(确保调试通过)

[WebMethod(Description="Login"]  //[System.Web.Services.  Protocols.SoapRpcMethod]  public bool Login(string   sLoginUserID,string sLoginPwd)  {   string spwd="";   gUserID = "";   if((sLoginUserID == null)   || (sLoginUserID.Trim() == ""))   {    return false;   }   try   {    myConnection = new SqlConnection(conStr);    string strSql = "SELECT * FROM tUser WHERE userid=@UserID";    SqlCommand myCommand = new SqlCommand(strSql, myConnection);    SqlParameter paramUserID =new SqlParameter("@UserID", SqlDbType.NVarChar, 12);    paramUserID.Value = sLoginUserID;    myCommand.Parameters.Add(paramUserID);    myConnection.Open();    dataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);    while(dataReader!=null && dataReader.Read())    {     spwd = dataReader.GetString(2);    }    if( !spwd.Equals(sLoginPwd))    {     return false;    }    else    {          return true;    }   }   catch (Exception ex)   {       Error.Log(ex.Message.ToString());    return false;   }   finally   {    if(myConnection!=null)     myConnection.Close();    if(dataReader!=null)     dataReader.Close();     }  }

上一个:JSP连接mysql数据库攻略
下一个:详细解析JSP编程中进度条的设计实例 (2)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,