最大的網(wǎng)站源碼資源下載站,
本文示例源代碼或素材下載
題外話:.webservice技術(shù)已經(jīng)有好幾年的歷史了,關(guān)于基礎(chǔ)的理論知識(shí),此處省去,不說(shuō)了。最近被炒了的ajax技術(shù)也被濫用的很是嚴(yán)重,至于細(xì)節(jié),不是我說(shuō)的重點(diǎn)。
ajax技術(shù)的入門(mén)比較低,javascript和xml的一點(diǎn)利用,個(gè)人以為沒(méi)啥含量,不能在根本上解決企業(yè)的需求或者問(wèn)題,在安全性,線路傳輸?shù)牡图?jí)等方面都有致命的問(wèn)題.
為什么要把webservice和ajax放一起,原因很簡(jiǎn)單,兩者的共同點(diǎn)是xml,準(zhǔn)確的來(lái)說(shuō)是經(jīng)常按照同一數(shù)據(jù)協(xié)議--------xml.
本文既不說(shuō)明ajax的弱,也不說(shuō)webservice的強(qiáng)大,只是通過(guò)一個(gè)例子說(shuō)明了如何使用webservice技術(shù)和ajax技術(shù).
我通過(guò)實(shí)例“用戶登陸”展示how.
在本地建立一個(gè)虛擬目錄userapp。
先創(chuàng)建一個(gè)webservice:
namespacecjjer{
usingsystem;
usingsystem.web.services;
[webservice(name="webscjjer",description="一個(gè)驗(yàn)證用戶登陸的web服務(wù)",namespace="http://www.cjjer.com/webs/")]
publicclassuserservice{
[webmethod(description="輸入用戶名和密,返回權(quán)限int值,0表示失敗",messagename="login")]
publicintlogin(stringusername,stringuserpassword){
returnuser.check(username,userpassword);
}
};
}
我們暫時(shí)不看細(xì)節(jié),注意這是這個(gè)class有頭webservice,標(biāo)記了這是一個(gè)web服務(wù).
csc編譯掉,放入bin目錄,然后login.asmx文件:
<%@webserviceclass="cjjer.userservice"%>
我先不說(shuō)驗(yàn)證的細(xì)節(jié),我們先假設(shè)user.check()返回的是用戶級(jí)別,0表示驗(yàn)證失敗.
|||注冊(cè)會(huì)員,創(chuàng)建你的web開(kāi)發(fā)資料庫(kù),現(xiàn)在在url中輸入地址,訪問(wèn):如圖
點(diǎn)擊login:
輸入正確的用戶名和密碼,然后點(diǎn)擊調(diào)用:
如果不是正確的用戶名和密碼,將int節(jié)點(diǎn)值是0.
我們本地的調(diào)用是在soap操作.
還有很多方式:
soap(最低級(jí)的)
soap的這里不說(shuō),以后會(huì)詳細(xì)說(shuō),我這里注意看的是get和post方式:
httpget
面是一個(gè)httpget請(qǐng)求和響應(yīng)示例。所顯示的占位符需要由實(shí)際值替換。
get/userapp/login.asmx/login?username=string&userpassword=stringhttp/1.1
host:s
return:
http/1.1200ok
content-type:text/xml;charset=utf-8
content-length:length
<?xmlversion="1.0"encoding="utf-8"?>
<intxmlns="http://www.cjjer.com/webs/">int</int>
httppost
下面是一個(gè)httppost請(qǐng)求和響應(yīng)示例。所顯示的占位符需要由實(shí)際值替換。
post/userapp/login.asmx/loginhttp/1.1
host:s
content-type:application/x-www-form-urlencoded
content-length:length
username=string&userpassword=string
return:
http/1.1200ok
content-type:text/xml;charset=utf-8
content-length:length
<?xmlversion="1.0"encoding="utf-8"?>
<intxmlns="http://www.cjjer.com/webs/">int</int>
我們可以輸入url的地址,或者post的action,就可以訪問(wèn)和提交獲取結(jié)果了.
此處可以選擇的操作其實(shí)也很多,使用屏幕清理技術(shù),還可以用xml的處理框架提交信息的結(jié)果,也可以用其他技術(shù),不說(shuō)了,反正都是貌似需要幾十行或者比我下面多一些處理。
我說(shuō)的是使用sdk的工具wsdl工具處理代理的web服務(wù).
wsdl/l:cs/out:webscjjer.cs/namespace:webs/urlkey:websuser/nologohttp://s:81/userapp/login.asmx?wsdl
得到webscjjer.cs源文件。
注意:/urlkey:websuser表示是url可以是從web.config文件的websuser獲取,這樣做是避免硬編碼,看看webscjjer.cs
.......
publicwebscjjer(){
stringurlsetting=system.configuration.configurationsettings.appsettings["websuser"];
if((urlsetting!=null)){
this.url=urlsetting;
}
else{
this.url="http://s:81/userapp/login.asmx";
}
}
.......
:
為了能被其他的人訪問(wèn),需要加入如下代碼到web.config:
<system.web>
<webservices>
<protocols>
<addname="httppost"/>
<addname="httpget"/>
</protocols>
</webservices>
就是代理許可.
編譯了這個(gè)類(lèi),到bin,使用的時(shí)候如下:
voidonlogin(objects,eventargse){
webs.webscjjerws_app=newwebs.webscjjer();
returnint.text=ws_app.login(myusername.text,mypassword.text).tostring();
}
這樣,直接能返回結(jié)果,不需要我們自己正則或者xml處理,不需要知道驗(yàn)證的細(xì)節(jié)。
看看如下效果:
登陸界面:
如果登陸成功:
否則;
ajax需要的代碼可能要稍微多一點(diǎn):
functionajaxlogin(){
varwebsurl='http://s:81/userapp/login.asmx/login?';
if(!checkloadfocus(document.all.myusername) ) {
alert('用戶名不合法,請(qǐng)返回');
returnfalse;
}elseif(!checkloadfocus(document.all.myusername) ) {
alert('密碼不合法,請(qǐng)返回');
returnfalse;
}
varurlstr=websurl+'username='+document.all.myusername.value+'&userpassword='+document.all.mypassword.value;
ajaxlogin(urlstr);
returnfalse;
}
functioncheckloadfocus(thise){
varsreg='([a-za-z0-9]+){3,}$';
varreg=newregexp(sreg,"i");
try{
if(!reg.test(thise.value)){
thise.value='admin';
thise.style.background='#ffffbf';
thise.focus();
thise.select();
alert('提交不合法,請(qǐng)返回');
returnfalse;
}
thise.select();
thise.style.background='#fff';
}catch(e){}
returntrue;
}
functionajaxlogin(file){
varxmlobj=null;
if(window.xmlhttprequest){
xmlobj=newxmlhttprequest();
}elseif(window.activexobject){
xmlobj=newactivexobject("microsoft.xmlhttp");
}else{
return;
}
xmlobj.onreadystatechange=function(){
if(xmlobj.readystate==4){
varrstr;
varmylevl=parseint(xmlobj.responsexml.getelementsbytagname('int')[0].firstchild.data);
if(mylevl>0){
rstr=('登陸成功,權(quán)限是'+mylevl);
}else{
rstr=('登陸失敗');
}
alert(rstr);
}
}
xmlobj.open('get',file,true);
xmlobj.send('');
}
總結(jié):就著樣,如果發(fā)布了web服務(wù),就可以使用wsdl創(chuàng)建代理類(lèi),然后獲取數(shù)據(jù),或者使用ajax獲取,兩者在簡(jiǎn)單的功能實(shí)現(xiàn)上ajax可能在不支持.net的系統(tǒng)可以實(shí)現(xiàn)一些功能,但應(yīng)對(duì)企業(yè)級(jí)的需求,比方二進(jìn)制對(duì)象傳輸,安全性要求比較高的需求等。web服務(wù)注重的是公開(kāi)服務(wù)內(nèi)容并使用,ajax注重的是或者xml的數(shù)據(jù),在長(zhǎng)遠(yuǎn)的開(kāi)來(lái),web服務(wù)更加具有廣泛的使用前景,它在異步傳輸,分布式數(shù)據(jù)處理上占有很重要的一席之地,我只據(jù)了一個(gè)小小的例子以說(shuō)明這一點(diǎn)。
新聞熱點(diǎn)
疑難解答
圖片精選