C#開發(fā)WAP程序?qū)嵗?/h1>
2010-08-28 10:49:27來源:西部e網(wǎng)作者:

平時(shí)下班等女朋友總是要等很長(zhǎng)時(shí)間,想看書又沒有帶,只有玩手機(jī)游戲;GPRS上網(wǎng),好象很多內(nèi)容都是要付費(fèi)的,就只有下載鈴聲,什么圖片的,沒勁!最好能用手機(jī)看看小說,網(wǎng)絡(luò)上好象沒有免費(fèi)的WAP小說,辦公室的小妹妹也想用手機(jī)看小說,看來只有自己做一個(gè)了。
都說JAVA是開發(fā)WAP的利器,不過WAP需要這個(gè)“碼”轉(zhuǎn)到那個(gè)“碼”,真是麻煩,算了,看一下能不能用C#做一個(gè)WAP小站。
想來也很簡(jiǎn)單的,WAP就是用UTF8編碼嘛,好象還有什么WML之類的,WML在網(wǎng)上查查資料,就和XML差不多,平時(shí)用XML也蠻多的,WML也就簡(jiǎn)單了,C#直接可以設(shè)RESPONSE的編碼的,看來有戲了,先寫一個(gè)“Hello 老婆“吧,
//設(shè)置ContentType類型
Response.ContentType="text/vnd.wap.wml";
//設(shè)置編碼
Response.ContentEncoding=System.Text.Encoding.UTF8;
//以下是根據(jù)WML協(xié)議寫的內(nèi)容部份
WapContent="<?xml version=\"1.0\" encoding=\"UTF8\"?>\r\n";
WapContent+="<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"WapContent+=" <p>hello老婆 </p>\r\n";
WapContent+="</card>\r\n";
WapContent+="\r\n";
WapContent+="</wml>\r\n";
WapContent+="\r\n";
Response.Write(WapContent);
用手機(jī)試一下,OK
現(xiàn)在寫小說的WAP程序。
先從網(wǎng)上下一篇小說,存在目錄下,1.txt
不過小說太大了,必須先要有一個(gè)目錄,按2K分頁(yè)吧,好家伙,有200頁(yè),算了,來一個(gè)直接選頁(yè)數(shù)的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>就看這頁(yè)!<br/>\n";
WapContent+="</anchor>\n";
所有的代碼:
private void Page_Load(object sender, System.EventArgs e)
{
string WapContent;
string mC;
//在調(diào)試時(shí)用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\" \"

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()+"頁(yè)</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>就看這頁(yè)!<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\" \"

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=\"下一頁(yè)\">\r\n";
WapContent+=" <go href=\"wap.aspx?Page="+(n+1).ToString()+"\"/>\r\n";
WapContent+=" </do>\n";
//選取頁(yè)面
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>就看這頁(yè)!<br/>\n";
WapContent+="</anchor>\n";
WapContent+="</card>\r\n";

WapContent+="\r\n";

WapContent+="</wml>\r\n";
WapContent+="\r\n";
Response.Write(WapContent);
}
拿起手機(jī),
www.tjkx.com/wap.aspx,試一下,爽。!
用手機(jī)調(diào)試太麻煩,下載一個(gè)WinWap Pro3.1在這里面調(diào)試。
工具齊備了,看來C#真好,寫個(gè)小程序就這樣簡(jiǎn)單

關(guān)鍵詞:C#

贊助商鏈接: