題外話:
webservice技術(shù)已經(jīng)有好幾年的歷史了,關(guān)于基礎(chǔ)的理論知識(shí),此處省去,不說了。最近被炒了的ajax技術(shù)也被濫用的很是嚴(yán)重,至于細(xì)節(jié),不是我說的重點(diǎn)。
ajax技術(shù)的入門比較低,javascript和xml的一點(diǎn)利用,個(gè)人以為沒啥含量,不能在根本上解決企業(yè)的需求或者問題,在安全性,線路傳輸?shù)牡图?jí)等方面都有致命的問題.
為什么要把webservice和ajax放一起,原因很簡(jiǎn)單, 兩者的共同點(diǎn)準(zhǔn)確的來說是經(jīng)常按照同一數(shù)據(jù)協(xié)議--------xml.
本文全部源碼:userapp.rar
本文既不說明ajax的弱,也不說webservice的強(qiáng)大,只是通過一個(gè)例子說明了如何使用webservice技術(shù)和ajax技術(shù).
我通過實(shí)例“用戶登陸”展示how.
在本地建立一個(gè)虛擬目錄userapp。
先創(chuàng)建一個(gè)webservice:
namespace cjjer{
using system;
using system.web.services;
[webservice (name="webscjjer",description="一個(gè)驗(yàn)證用戶登陸的web服務(wù)",namespace="http://www.cjjer.com/webs/")]
public class userservice{
[webmethod (description ="輸入用戶名和密,返回權(quán)限int值,0表示失敗",messagename="login")]
public int login(string username,string userpassword){
return user.check(username,userpassword);
}
};
}
我們暫時(shí)不看細(xì)節(jié),注意這是這個(gè)class有頭webservice,標(biāo)記了這是一個(gè)web服務(wù).
csc編譯掉,放入bin目錄,然后login.asmx文件:
<%@webservice class="cjjer.userservice"%>
我先不說驗(yàn)證的細(xì)節(jié),我們先假設(shè)user.check()返回的是用戶級(jí)別,0表示驗(yàn)證失敗.
現(xiàn)在在url中輸入地址,訪問:如圖
點(diǎn)擊login:
輸入正確的用戶名和密碼,然后點(diǎn)擊調(diào)用:
如果不是正確的用戶名和密碼,將int節(jié)點(diǎn)值是0.
我們本地的調(diào)用是在soap操作.
還有很多方式:
soap(最低級(jí)的)
soap的這里不說,以后會(huì)詳細(xì)說,我這里注意看的是get和post方式:
http get
面是一個(gè) http get 請(qǐng)求和響應(yīng)示例。所顯示的占位符需要由實(shí)際值替換。
get /userapp/login.asmx/login?username=string&userpassword=string http/1.1
host: s
return:
http/1.1 200 ok
content-type: text/xml; charset=utf-8
content-length: length
<?xml version="1.0" encoding="utf-8"?>
<int xmlns="http://www.cjjer.com/webs/">int</int>
http post
下面是一個(gè) http post 請(qǐng)求和響應(yīng)示例。所顯示的占位符需要由實(shí)際值替換。
post /userapp/login.asmx/login http/1.1
host: s
content-type: application/x-www-form-urlencoded
content-length: length
username=string&userpassword=string
return:
http/1.1 200 ok
content-type: text/xml; charset=utf-8
content-length: length
<?xml version="1.0" encoding="utf-8"?>
<int xmlns="http://www.cjjer.com/webs/">int</int>
我們可以輸入url的地址,或者post的action,就可以訪問和提交獲取結(jié)果了.
此處可以選擇的操作其實(shí)也很多,使用屏幕清理技術(shù),還可以用xml的處理框架提交信息的結(jié)果,也可以用其他技術(shù),不說了,反正都是貌似需要幾十行或者比我下面多一些處理。
我說的是使用sdk的工具wsdl工具處理代理的web服務(wù).
wsdl /l:cs /out:webscjjer.cs /namespace:webs /urlkey:websuser /nologo http://s:81/userapp/login.asmx?wsdl
得到webscjjer.cs源文件。
注意:/urlkey:websuser表示是url可以是從web.config文件的websuser獲取,這樣做是避免硬編碼,看看webscjjer.cs
.......
public webscjjer() {
string urlsetting = system.configuration.configurationsettings.appsettings["websuser"];
if ((urlsetting != null)) {
this.url = urlsetting;
}
else {
this.url = "http://s:81/userapp/login.asmx";
}
}
.......
為了能被其他的人訪問,需要加入如下代碼到web.config:
<system.web>
<webservices>
<protocols>
<add name="httppost"/>
<add name="httpget"/>
</protocols>
</webservices>
就是代理許可.
編譯了這個(gè)類,到bin,使用的時(shí)候如下:
void onlogin(object s,eventargs e){
webs.webscjjer ws_app = new webs.webscjjer();
returnint.text = ws_app.login(myusername.text,mypassword.text).tostring();
}
這樣,直接能返回結(jié)果,不需要我們自己正則或者xml處理,不需要知道驗(yàn)證的細(xì)節(jié)。
新聞熱點(diǎn)
疑難解答
圖片精選