C#開發WAP程序
2024-07-21 02:18:18
供稿:網友
平時下班等女朋友總是要等很長時間,想看書又沒有帶,只有玩手機游戲;gprs上網,好象很多內容都是要付費的,就只有下載鈴聲,什么圖片的,沒勁!最好能用手機看看小說,網絡上好象沒有免費的wap小說,辦公室的小妹妹也想用手機看小說,看來只有自己做一個了。
都說java是開發wap的利器,不過wap需要這個“碼”轉到那個“碼”,真是麻煩,算了,看一下能不能用c#做一個wap小站。
想來也很簡單的,wap就是用utf8編碼嘛,好象還有什么wml之類的,wml在網上查查資料,就和xml差不多,平時用xml也蠻多的,wml也就簡單了,c#直接可以設response的編碼的,看來有戲了,先寫一個“hello 老婆“吧,
//設置contenttype類型
response.contenttype="text/vnd.wap.wml";
//設置編碼
response.contentencoding=system.text.encoding.utf8;
//以下是根據wml協議寫的內容部份
wapcontent="<?xml version=/"1.0/" encoding=/"utf8/"?>/r/n";
wapcontent+="<!doctype wml public /"-//wapforum//dtd wml 1.1//en/" /"http://www.wapforum.org/dtd/wml_1.1.xml/"><wml>/r/n";
wapcontent+="/r/n";
wapcontent+="<card id=/"hello/">/r/n";
wapcontent+=" <p>hello老婆 </p>/r/n";
wapcontent+="</card>/r/n";
wapcontent+="/r/n";
wapcontent+="</wml>/r/n";
wapcontent+="/r/n";
response.write(wapcontent);
用手機試一下,ok
現在寫小說的wap程序。
先從網上下一篇小說,存在目錄下,1.txt
不過小說太大了,必須先要有一個目錄,按2k分頁吧,好家伙,有200頁,算了,來一個直接選頁數的go吧:
wapcontent+=" <input name=/"pagenum/" size=/"3/" maxlength=/"10/" type=/"text/" format=/"*n/" emptyok=/"true/"/>";
wapcontent+="<anchor title=/"login/">/n";
wapcontent+="<go href=/"wap.aspx/" method=/"get/">/n";
wapcontent+="<postfield name=/"page/" value=/"$pagenum/"/>/n";
wapcontent+="</go>就看這頁!<br/>/n";
wapcontent+="</anchor>/n";
所有的代碼:
private void page_load(object sender, system.eventargs e)
{
string wapcontent;
string mc;
//在調試時用gb2312模擬器
response.contenttype="text/vnd.wap.wml";
// response.contenttype="text/vnd.wap.wml;charset=utf-8";
response.contentencoding=system.text.encoding.utf8;// unicode ;// "utf8";
// response.contentencoding=system.text.encoding.getencoding("gb2312");
int n=0;
if(request["page"]==null)
{
//顯示目錄
wapcontent="<?xml version=/"1.0/" encoding=/"utf8/"?>/r/n";
// wapcontent="<?xml version=/"1.0/" encoding=/"gb2312/"?>/r/n";
wapcontent+="<!doctype wml public /"-//wapforum//dtd wml 1.1//en/" /"http://www.wapforum.org/dtd/wml_1.1.xml/"><wml>/r/n";
wapcontent+="/r/n";
system.io.fileinfo ofm=new system.io.fileinfo(request.physicalpath);
system.io.streamreader oreaderd=new system.io.streamreader(ofm.directoryname +"//"+ "1.txt",system.text.encoding.default);
//每次讀3000字
mc=oreaderd.readtoend() ;
oreaderd.close();
wapcontent+="<card id=/"lists"+n.tostring()+"/">/r/n";
int k=mc.length/3000;
wapcontent+=" <p>目錄</p>/r/n";
wapcontent+=" <p>共:"+k.tostring()+"頁</p>/r/n";
wapcontent+=" <input name=/"sid/" size=/"3/" maxlength=/"10/" type=/"text/" format=/"*n/" emptyok=/"true/"/>";
wapcontent+="<anchor title=/"login/">/n";
wapcontent+="<go href=/"wap.aspx/" method=/"get/">/n";
wapcontent+="<postfield name=/"page/" value=/"$sid/"/>/n";
wapcontent+="</go>就看這頁!<br/>/n";
wapcontent+="</anchor>/n";
wapcontent+="<onevent type=/"onenterbackward/">/r/n";
wapcontent+=" <prev/>/r/n";
wapcontent+=" </onevent>/r/n";
wapcontent+="</card>/r/n";
wapcontent+="/r/n";
wapcontent+="</wml>/r/n";
wapcontent+="/r/n";
response.write(wapcontent);
return;
}
else
n=system.convert.toint32(request["page"]);
wapcontent="<?xml version=/"1.0/"?>/r/n";
wapcontent+="<!doctype wml public /"-//wapforum//dtd wml 1.1//en/" /"http://www.wapforum.org/dtd/wml_1.1.xml/"><wml>/r/n";
wapcontent+="/r/n";
system.io.fileinfo of=new system.io.fileinfo(request.physicalpath);
system.io.streamreader oreader=new system.io.streamreader(of.directoryname +"//"+ "1.txt",system.text.encoding.default);
//每次訊讀3000字
mc=oreader.readtoend() ;
oreader.close();
if(mc.length>4000)
{
mc=mc.substring((n)*3000,3000);
mc=mc.replace("/r/n","<br/>");
artmore=true;
}
//每屏3000字
wapcontent+="<card id=/"lists"+n.tostring()+"/">/r/n";
wapcontent+=" <do type=/"accept/" label=/""+(n-1).tostring()+"/">/r/n";
wapcontent+=" <go href=/"wap.aspx?page="+(n-1).tostring()+"/"/>/r/n";
wapcontent+=" </do>/r/n";
wapcontent+="<onevent type=/"onenterbackward/">/r/n";
wapcontent+=" <prev/>/r/n";
wapcontent+=" </onevent>/r/n";
wapcontent+=" <p>/r/n";
wapcontent+=n.tostring()+"p";
wapcontent+=mc;
wapcontent+=" </p>/r/n";
wapcontent+=" <do type=/"accept/" label=/"下一頁/">/r/n";
wapcontent+=" <go href=/"wap.aspx?page="+(n+1).tostring()+"/"/>/r/n";
wapcontent+=" </do>/n";
//選取頁面
wapcontent+=" <input name=/"sid/" size=/"3/" maxlength=/"10/" type=/"text/" format=/"*n/" emptyok=/"true/"/>";
wapcontent+="<anchor title=/"login/">/n";
wapcontent+="<go href=/"wap.aspx/" method=/"get/">/n";
wapcontent+="<postfield name=/"page/" value=/"$sid/"/>/n";
wapcontent+="</go>就看這頁!<br/>/n";
wapcontent+="</anchor>/n";
wapcontent+="</card>/r/n";
wapcontent+="/r/n";
wapcontent+="</wml>/r/n";
wapcontent+="/r/n";
response.write(wapcontent);
}
拿起手機,www.tjkx.com/wap.aspx,試一下,爽!!!
用手機調試太麻煩,下載一個winwap pro3.1在這里面調試。
工具齊備了,看來c#真好,寫個小程序就這樣簡單
中國最大的web開發資源網站及技術社區,