都說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#真好,寫個小程式就這樣簡單